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 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
How does the following compare to a join statement?
SELECT t1.name, t2.salary
FROM employee t1, info t2
WHERE t1.name = t2.name;
would it be the equivalent to this?
SELECT t1.name, t2.salary
FROM employee t1,
INNER JOIN info t2
ON t1.name = t2.name;
or is it more like an outer join?
it will be like this without comma.
SELECT t1.name, t2.salary
FROM employee t1
INNER JOIN info t2
ON t1.name = t2.name;
To use INNER JOIN or LEFT or RIGHT or ... its up to you what results you want get.
related values or values which exist in other table and so on , here you can learn about the joins.
SOURCE
table 1 includes employee name and their initials
table 2 includes projectnumber and pmember(initials as well)
we only have to display the name of the employees so i have to use a WHERE clause, which i can't construct :( here's what i've done so far. it only returns 1 employee (correct answer is 2 employees)
select t1.name
from t1
where t1.initials IN (select t2.pmember
from t2
having count(t2.projectnumber) > 1)
thanks in advance!
select t1.name
from t1
left join t2 on t1.initials = t2.pmember
group by t1.name
having sum(t2.projectnumber is null) = 0
This query should give you the names of the members working on all projects:
SELECT T1.NAME
FROM T1
JOIN T2 ON T1.INITIALS = T2.PMEMBER
GROUP BY T1.NAME
HAVING COUNT(T2.PROJECTNUMBER) = (SELECT COUNT(1) FROM T2 T2ALIAS)
Maybe this query helps you:
SELECT T1.NAME
FROM T1
INNER JOIN T2 ON T1.INITIALS = T2.PMEMBER
HAVING COUNT(T2.PROJECTNUMBER) > 1
To show all emp that works on all projects, you need a distinct count like this, I think:
SELECT NAME FROM T1 WHERE INITIALS IN (
SELECT PMEMBER FROM T2 HAVING DISTINCT COUNT(PROJECTNUMBER) = (
SELECT DISTINCT COUNT(PROJECTNUMBER) FROM T2)
GROUP BY PMEMBER)
I don't know how to make this.
Background:
Profiles -> table with user profiles
Score -> table with users' score (One user can have multiple scores) (This table has a created_at field to know when I introduce the new score)
Imagine that score table save klout score data.
How do convert this query to a view?
SELECT t1.id,
t1.name,
t1.screen_name,
t1.description,
t1.url,
t1.statuses_count,
t1.followers_count,
t1.friends_count,
t1.listed_count,
t1.favourites_count,
t1.utc_offset,
t1.time_zone,
t1.verified,
t1.lang,
t1.profile_image_url,
t1.geo_enabled,
t1.location,
t1.lat,
t1.lng,
t1.created_at,
t3.score,
t3.delta,
t3.detail
FROM profiles t1 LEFT JOIN (SELECT t2.user_id, t2.score, t2.delta, t2.detail
FROM scores t2
ORDER BY t2.created_at DESC)
AS t3 ON t3.user_id = t1.id
GROUP BY t1.id
The problem VIEW cannot contain subquery, the subquery in your query can be directly join on the table since you haven't perform any aggregation.
CREATE VIEW viewName
AS
SELECT t1.id,
t1.name,
t1.screen_name,
t1.description,
t1.url,
t1.statuses_count,
t1.followers_count,
t1.friends_count,
t1.listed_count,
t1.favourites_count,
t1.utc_offset,
t1.time_zone,
t1.verified,
t1.lang,
t1.profile_image_url,
t1.geo_enabled,
t1.location,
t1.lat,
t1.lng,
t1.created_at,
t3.score,
t3.delta,
t3.detail
FROM profiles t1
LEFT JOIN scores t3
ON t3.user_id = t1.id
-- GROUP BY t1.id
I can't get why you have a GROUP BY clause in your query.
You can split your code into 2 views
View 1:
CREATE VIEW step1
AS
SELECT t2.user_id, t2.score, t2.delta, t2.detail
FROM scores t2
ORDER BY t2.created_at DESC
View 2:
CREATE VIEW step2
AS
SELECT t1.id,
t1.name,
t1.screen_name,
t1.description,
t1.url,
t1.statuses_count,
t1.followers_count,
t1.friends_count,
t1.listed_count,
t1.favourites_count,
t1.utc_offset,
t1.time_zone,
t1.verified,
t1.lang,
t1.profile_image_url,
t1.geo_enabled,
t1.location,
t1.lat,
t1.lng,
t1.created_at,
t3.score,
t3.delta,
t3.detail
FROM profiles t1
LEFT JOIN step1 AS t3 ON t3.user_id = t1.id
GROUP BY t1.id
I am not sure why you are grouping by t1.id, but maybe there's something in your data that I don't know about