0

I have three tables. One of the table is main table(TRIP). The other two tables are having foreign key to the main table. I want to query data which are in both tables and the condition on each table. Here is my code:

SELECT L#
FROM TRIP 
WHERE REG# IN 
(
    SELECT REG#
    FROM TRUCK
    WHERE REG# = 'PKR768'
)
AND T# IN
(
    SELECT T#
    FROM TRIPLEG
    WHERE TRIPLEG.DEPARTURE = 'Melbourne'
) 

ERROR = missing right parenthesis

AFTER EDIT:

THERE IS ONLY ONE ROW RETURNED FROM TRUCK TABLE SO THE OUTPUT OF WHOLE SHOULD NOT BE MORE THAN 1 ROW BUT IT IS MORE THAN 1.

2 Answers 2

2

Caveat I am not particularly familiar with mysql but I would express this query more naturally as

select tp.l#
from truck t
inner join trip tr on tr.reg# = t.reg#
inner join tripleg tl on tl.t# = tr.t# and tl.departure = 'Melbourne'
where t.reg# = 'PKR768' 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, it works fine but I wondered why if I add tl.departure = 'Melbourne' to the where condition it is not working. Any idea?
1

You are missing a WHERE clause in the second query. Change it to:

SELECT T#
FROM TRIPLEG
WHERE TRIPLEG.DEPARTURE = 'Melbourne'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.