select t1.table1 from table1 as t1
where t1.column1
in
(
select t2.column2 from table2 as t2
join
table3 as t3 on t2.column1=t3.column1
where t3.columnx=5
);
Above is the mysql query i am firing. Wanted some data from the subquery tables also.
For example say columnxy from table t2.
query that fails
select t1.table1,t2.columnxy from table1 as t1
where t1.column1
in
(
select t2.column2 from table2 as t2
join
table3 as t3 on t2.column1=t3.column1
where t3.columnx=5
);
If i add them with select of the outer query gives error "unknown column" which does make sense.
Is the right way or should rewrite query with joins?
Rewrite the query with joins:
SELECT t1.table1, t.columnxy
FROM table1 AS t1 JOIN (
SELECT t2.column2, t2.columnxy
FROM table2 AS t2 JOIN table3 AS t3 USING (column1)
WHERE t3.columnx = 5
) t ON t1.column1 = t.column2
Or:
SELECT t1.table1, t2.columnxy
FROM table1 AS t1
JOIN table2 AS t2 ON t1.column1 = t2.column2
JOIN table3 AS t3 ON t2.column1 = t3.column1
WHERE t3.columnx = 5
The t2 is not available at that point. You should use a join for this. Using t1.column1=t2.column2 should do it.
Related
I basically want to join the result of two INNER JOINs.
On this scheme I want to get the three arrows results combined.
I've tried INNER / LEFT combinations but it doesn't do the trick.
I think a nested request could be the solution but how ?
Thanks
The answer was actually simple : UNION
SELECT t1.*
FROM
(SELECT t1.*
FROM table1 t1 JOIN table2 t2 ON t2.id = i.client_id
UNION
SELECT t1.*
FROM t1 t1 JOIN table3 t3 ON t1.id = t3.client_id) as q1
;
I'd use logic to express the condition T1.id exists in T2 or T3 more directly, and certainly avoid use of DISTINCT or UNION.
Options could be to use EXISTS directly (As this is immure to the possibility of duplication cause by 1:many joins)...
SELECT
t1.*
FROM
table1 t1
WHERE
EXISTS (SELECT * FROM table2 t2 WHERE t2.t1_id = t1.id)
OR
EXISTS (SELECT * FROM table3 t3 WHERE t3.t1_id = t1.id)
Or to LEFT JOIN twice and then exclude unwanted rows. (This assumes that the joins are never 1:many, which would introduce duplication, and the unwanted need for a DISTINCT.)
SELECT
t1.*
FROM
table1 t1
LEFT JOIN
table2 t2
ON t1.id = t2.t1_id
LEFT JOIN
table3 t3
ON t1.id = t3.t1_id
WHERE
t2.t1_id IS NOT NULL
OR
t3.t1_id IS NOT NULL
I'm trying to JOIN a table with a subquery to limit data to only 1 last row, matching certain values from other FROM or JOINed tables:
SELECT
t1.column1,
t1.column2,
t2.column1,
t3.column2
FROM
table1 t1
JOIN
(
SELECT column1
FROM table2
WHERE t1.column2 > table2.column1
ORDER BY table2.date DESC
LIMIT 1
) t2
JOIN table3 t3 ON t2.column1=t3.column2
Getting an error: Unknown column t1.column2 in 'where clause'. Seems I can't address other columns inside the subquery.
Is what I'm trying to do possible?
If so, what am I doing wrong / what other way could I try this?
In MySQL, implicit and explicit joins together in a single query create the problem. You can not use an implicit JOIN based on a nested SELECT - FROM - WHERE query when there is another explicit JOIN present.
Try below.
SELECT *
FROM table1 t1
INNER JOIN table2 t2 ON t1.column2 > t2.column1
LEFT OUTER JOIN table3 t3 ON t2.column1=t3.column2
You may re-write your query as -
SELECT t1.column1,
t1.column2,
t2.column1,
t3.column2
FROM table1 t1
JOIN (SELECT column1
FROM table2
ORDER BY table2.date DESC
LIMIT 1) t2 ON t1.column2 > t2.column1
JOIN table3 t3 ON t2.column1=t3.column2
The solution for me was:
SELECT
t1.column1,
t1.column2,
t2.column1,
t3.column2
FROM
table1 t1
JOIN table2 t2 ON t2.id =
(
SELECT id
FROM table2
WHERE t1.column2 > table2.column1
ORDER BY table2.date DESC
LIMIT 1
)
JOIN table3 t3 ON t2.column1=t3.column2
More info on:
CROSS/OUTER APPLY in MySQL
https://dba.stackexchange.com/a/170760/13156
I have 2 tables and result as shown in the image below: MySQL DB
What would be best way to join the two tables so we get the result as shown above.
SELECT * FROM (SELECT id, desc FROM table2) as T1
LEFT JOIN (SELECT * FROM table1) as T2 ON T1.id = T2.id
I guess my SQL is not working.
You can use a LEFT JOIN with COALESCE:
SELECT t1.id, COALESCE(t2.desc, t1.desc) AS desc, t1.D1, t1.D2
FROM table1 as T1
LEFT JOIN table2 as T2 ON T1.id = T2.id
Use a left join with coalesce to prioritize table 2's values if they are present, but fallback on table 1's values if not.
select t1.id,
coalesce(t2.desc, t1.desc) as desc,
t1.d1, t1.d2
from table1 t1
left join table2 t2
on t2.id = t1.id
order by t1.id
You can use ifnull:
SELECT t1.id, ifnull(t2.desc, t1.desc) AS desc, t1.D1, t1.D2
FROM table1 as T1
LEFT JOIN table2 as T2 ON T1.id = T2.id
coalesce or case .. when is also possible. All together with the left join
I have a query which contains a select statement in it's where clause. My question is now, how can I access the parent's select's data.
Example:
select * from TABLE_1 as t1 INNER JOIN TABLE_2 as t2
where (... and ...) OR
(not exists(select * from TABLE_3 as t3
inner join TABLE_1 ON t3.t1_id = t1.id
The last line is where the error occurs: t1.id is not a column.
How can I access the current value from the table t1?
I'm using MySql 5.1
SELECT
*
FROM
TABLE_1 as t1
INNER JOIN TABLE_2 as t2 ON
t2.PK = t1.FK --Whatever your keys are
WHERE
(... and ...)
OR
(
NOT EXISTS (select * from TABLE_3 as t3 WHERE t3.t1_id = t1.id)
)
First of all, you need to declare what you will JOIN TABLE_2 on TABLE_1.
SELECT *
FROM TABLE_1 AS t1
INNER JOIN TABLE_2 AS t2 ON t2.t1_id = t1.id
t1.id = t2.t1_id is just an example, you will need to decide which columns you wish you join on. Then in your WHERE clause, you do not need to INNER JOIN on TABLE_1 again as you are already selecting from it.
SELECT *
FROM TABLE_1 AS t1
INNER JOIN TABLE_2 AS t2 ON t2.t1_id = t1.id
WHERE (... AND ...) OR
(
NOT EXISTS
(
SELECT *
FROM TABLE_3 AS t3
WHERE t3.t1_id = t1.id
)
)
Query 1:
SELECT if(COUNT(0),1,0) as 'IsPresent'
FROM table1
WHERE Id=1500;
Query2:
If IsPresent is 1, then
select t2.mark,t2.age from table2 t2,table1 t1
where t1.ID=t2.ID order by t1.ID;
If IsPresent is 0, then
select mark,age from table2;
ie. if entry is present in a table, i need to join else i don't need to join.
Is there any way we can achieve this with a single mysql select query?
I think you can union the two different query cases which would look like:
SELECT T2.MARK, T2.AGE
FROM TABLE1 T1, TABLE2 T2
WHERE
T1.ID=T2.ID AND
T1.ID=1500
UNION
SELECT MARK, AGE
FROM TABLE1
WHERE
NOT ID=1500
SELECT t2.mark, t2.age
FROM table2 t2
JOIN table1 t1
ON t1.id = t2.id
WHERE EXISTS
( SELECT *
FROM table1
WHERE id=1500
)
UNION ALL
SELECT t2.mark, t2.age
FROM table2 t2
WHERE NOT EXISTS
( SELECT *
FROM table1
WHERE id=1500
)
which can be simplified to:
SELECT t2.mark, t2.age
FROM table2 t2
LEFT JOIN table1 t1
ON t1.id = t2.id
AND EXISTS
( SELECT *
FROM table1
WHERE id=1500
)