Issue constructing SQL join query - sql

I'm trying to construct an SQL query that selects data from both tables but it doesn't seem to be working:
SELECT DISTINCT name,$price
FROM room
WHERE capacity>=$partySize
JOIN room_booking ON room.room_id=room_booking.room_id
WHERE date_booked<>'$us_date';
What am I doing wrong?

Move the WHERE clause down to the bottom:
SELECT DISTINCT name,$price
FROM room
JOIN room_booking ON room.room_id=room_booking.room_id
WHERE date_booked<>'$us_date' AND capacity>=$partySize

Definitely read some more SQL tutorials. You cannot use two WHERE conditions in one query:
SELECT DISTINCT name,'$price'
FROM room
JOIN room_booking ON room.room_id=room_booking.room_id
WHERE date_booked<>'$us_date' AND capacity>='$partySize';
Also, variables in the query should be quoted.

try
SELECT DISTINCT r.name,$price
FROM room r, room_booking b
WHERE r.capacity>=$partySize
and r.room_id=b.room_id
and r.date_booked<>'$us_date';
What's $price? is it a value? if variable try r.$price

Related

How to join two tables for particular column without using joins

I am running a query on two different tables:-
select * from MessagesTable INNER JOIN ErrorTable ON MessagesTable.msgId = ErrorTable.msgId.
Is there an another way to write this query without using JOIN?
If you would not use join in query you can write sub query for this.
Below query for SQL DATABASE-
select *
from MessagesTable
where MessagesTable.msgId in (select ErrorTable.msgId from ErrorTable)
I don't know why do you want to do it but YES you can do it as long as you want data from only MessagesTable. Its called sub query.
select * from MessagesTable WHERE msgId IN (select msgId from ErrorTable)
Yes, you can use this query in place of join for particular column
select * from MessagesTable,ErrorTable where MessagesTable.msgId =ErrorTable.msgId

How can I prevent Duplicates from this SQL Statement?

I have two tables 1. tdppackages and 2. tpdstop and I do a SQL SELECT INNER JOIN to create a TableAdapter with some info from both and I want to NOT add duplicate records. Here is my SQL Statement:
SELECT tdppackages.trackno,
tdppackages.shpmentno,
tpdstop.custname,
tpdstop.address,
tpdstop.city,
tdppackages.amtdue,
tpdstop.pkgs,
tpdstop.ndx
FROM tpdstop
INNER JOIN tdppackages ON tpdstop.ndx = tdppackages.stopkey
Change SELECT to SELECT DISTINCT is the fastest way.
I think you would be having composite key which you should include in on clause.
like
INNER JOIN tdppackages ON tpdstop.ndx = tdppackages.stopkey
And tpdstop.col2 = tdppachages.col2
Do LEFT JOIN instead. Knowing the differences of each join would be very helpful moving forward.

How to edit values in sql access with Distinct?

How to edit values in sql access with Distinct?
SELECT DISTINCT ProdutosOrcamento.IDProdutoOrc, ProdutosOrcamento.Produto, ProdutosOrcamento.PrecoOrc, ProdutosOrcamento.IPIOrc, detOrcamentos.IDOrcamento
FROM ProdutosOrcamento LEFT JOIN detOrcamentos ON ProdutosOrcamento.IDProdutoOrc=detOrcamentos.IDProdutoOrc
WHERE (((detOrcamentos.IDOrcamento)=[Formulários]![Orcamentos]![IDOrcamento]));
If you have a DISTINCT (or GROUP BY for that matter) in your SQL statement, MS Access will consider the query to be read-only.
A possible work-around is to make a new simple editable SELECT query that usea an IN() statement on the ID field.
EDIT:
SELECT * FROM ProdutosOrcamento WHERE ProdutosOrcamento.IDProdutoOrc IN
(SELECT ProdutosOrcamento.IDProdutoOrc
FROM ProdutosOrcamento LEFT JOIN detOrcamentos ON ProdutosOrcamento.IDProdutoOrc=detOrcamentos.IDProdutoOrc
WHERE (((detOrcamentos.IDOrcamento)=[Formulários]![Orcamentos]![IDOrcamento])))

Need help forming SQLite join query

I've been able to make a query using MS Access 2010 that does just what I want but I am having trouble doing so in SQLite3. Here is the Access SQL
SELECT pubacc_lo.*
FROM pubacc_en
LEFT JOIN pubacc_lo ON pubacc_en.call_sign = pubacc_lo.call_sign;
Basically it selects all of the columns in the pubacc_lo table and the rows where the call_sign fields are equal between the tables. This does not select any of the pubacc_en data into the final query in MS Access.
Playing around in SQLite 3, the closest I've gotten was
SELECT * FROM PUBACC_LO, PUBACC_EN WHERE PUBACC_en.call_sign=PUBACC_LO.call_sign
But this statement selects all of the data in the EN table along with the LO table (cross join?). I've tried some left outer joins but haven't had any luck. Any tips would be appreciated!
You should be able to use the same query as you got from Access. SQLite3 does support left outer joins.
As for your query, if you only want the fields from the LO table, then ask for that in your SELECT clause like this:
SELECT PUBACC_LO.*
FROM PUBACC_LO, PUBACC_EN
WHERE PUBACC_en.call_sign=PUBACC_LO.call_sign
but the problem here is that it will only return call_signs with entries in both tables, while the outer join from access will return all rows from PUBACC_EN irrespective of whether there is a corresponding PUBACC_LO entry..
SELECT pubacc_lo.* FROM PUBACC_LO, PUBACC_EN WHERE UBACC_en.call_sign=PUBACC_LO.call_sign
If you only want to select pubbac_lo's field, this is what you can use.

problem with IN clause in SQL

We have observed that there seems to be a maximum number of ids/variables which one can pass in the IN clause of SQL as comma separated values. To avoid this we are storing all the ids in a table and doing a SELECT within the IN clause. This however means extra database operations to store and retrieve ids. Is there any other way to use IN without SELECT?
Regards,
Sameer
In SQL Server 2008 you can declare a table variable and pass it to your query from the client or between the procedures.
For a modest number of values I would not have thought an IN (SELECT ..) would be that expensive on any rdbms.
You could INNER JOIN you IDs table or just break down the INs:
WHERE X IN (123,456 ...)
OR X IN (789,987 ...)
...
Agree with Alex
Instead of
Select * From TableA Where ID IN (Select ID from IDTable)
Use
Select * From TableA
INNER JOIN IDTable ON TableA.ID = IDTable.ID
The join will automatically filter the IDs for you.
If you put it in atable why are you still using the in clause, why not just join to the table?