Issues with SQL queries - mysql

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

Related

mySQL group two INNER JOINs

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

how to return all the fields of table2 based upon the occurrence of the id in the table1

I have 2 tables, one is table1
and another is table 2
I want the result by a query, like
I have tried select id from table2 order by (select id from table1); but it is giving error.
You can join and sort. But you need a column that defines the ordering of the rows in table1. Let me assume that you have such column, and that is is called ordering_id.
select t2.*
from table2 t2
inner join table1 t1 on t1.id = t2.id
order by t1.ordering_id
You can even use a subquery in the order by clause:
select *
from table2 t2
order by (select t1.ordering_id from table1 t1 where t1.id = t2.id)
Join the two tables and then order the result.But for that you need to have some column for ordering and this does not seems to be the case. Syntax you are using for ordering will not work.
SELECT A.ID, B.NAME FROM TABLE1 A INNER JOIN TABLE2 B
ON(A.ID = B.ID) ORDER BY A.ID DESC
finally got the answer
select t2.*
from table2 t2
inner join table1 t1 on t1.id = t2.id;

JOIN a table by SELECT subquery with LIMIT 1 and WHERE clause matching column values outside of it

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

inner join between 2 tables - odd syntax?

see the following SQL..
SELECT t1.*
FROM table1 t1
INNER JOIN table2 t2 ON t1.user_id = t2.id, tables3 t3
WHERE t3.id = 999
what kind of join is there here? between t1 and t3?. I mean what is the comma all about?
A , in the FROM clause is a deprecated shorthand for CROSS JOIN
A better way to write the query is with an explicit CROSS JOIN:
SELECT t1.*
FROM table1 t1 INNER JOIN
table2 t2
ON t1.user_id = t2.id CROSS JOIN
tables3 t3
WHERE t3.id = 999 ;
As written, the query makes no sense. Assuming that t3.id = 999 is true and appears once, then this is equivalent to:
SELECT t1.*
FROM table1 t1 INNER JOIN
table2 t2
ON t1.user_id = t2.id;
If the value doesn't exist, then no rows will be returned.
SELECT t1.*
FROM table1 t1
JOIN table2 t2 ON t1.user_id = t2.id
JOIN tables3 t3 ON t2.id=t3.id
WHERE t3.id = 999

MySQL Joining Results of Queries

I am trying to find rows if the second query that do not exist in the first. But I get the 'You have error in your SQL syntax near a right join' error.
(SELECT t1.id AS id, t2.id, t3.id
FROM table1 t1
INNER JOIN table2 t2 ON t2.id = t1.id
INNER JOIN table3 t3 ON t3.id = t2.id
) a
RIGHT JOIN
(SELECT id
FROM table4
WHERE col1 IS NOT NULL AND col2 IN (1, 2)) b
ON a.id = b.id
What do I do wrong in this query? Thank you.
In join queries, you must put all your columns in the first select statement. The second select statement in your query is invalid.
SELECT
t1.id as id, t2.id, t3.id, b.id
FROM
(table1 t1
inner join table2 t2 on t2.id=t1.id
inner join table3 t3 on t3.id=t2.id)
right join table4 b on t1.id = b.id
WHERE b.col1 IS NOT NULL AND b.col2 IN (1, 2))