I have 5 tables.
I want to get common users in table 1, 2 and 3 that are not in table 4 and 5.
Can someone please help me :)
Tables
table1(userid,discount)
table2(userid,discount)
table3(userid,discount)
table4(userid,discount)
table5(userid,discount)
One way, left join on the table rows to omit:
select *
from table1 a
join table2 b on (a.userid = b.userid)
join table3 c on (a.userid = c.userid)
left join table4 d on (a.userid = d.userid)
left join table5 e on (a.userid = e.userid)
where d.userid is null and e.userid is null;
Getting the users common to tables 1, 2, 3 is easy -- just do an inner join. To get all of those users which are not in tables 4 or 5, you could test for their non-existence in these tables in the where clause.
select *
from table1
join table2 on table1.userid = table2.userid
join table3 on table1.userid = table3.userid
where
not exists (select * from table4 where table4.userid = table1.userid)
and not exists (select * from table5 where table5.userid = table1.userid)
Try this query
SELECT t1.userid, t2.userid, t2.userid, t4.userid, t5.userid
FROM table1 t1
LEFT JOIN table2 t2 ON (t2.userid = t1.userid)
LEFT JOIN table3 t3 ON (t3.userid = t1.userid)
LEFT JOIN table4 t4 ON (t4.userid = t1.userid)
LEFT JOIN table5 t5 ON (t5.userid = t1.userid)
GROUP BY t1.userid
HAVING t1.userid IS NOT NULL
AND t2.userid IS NOT NULL
AND t3.userid IS NOT NULL
AND t4.userid IS NULL
AND t5.userid IS NULL
SELECT *
FROM tableA a
JOIN tableB b ON (a.userid = b.userid)
JOIN tableC c ON (a.userid = c.userid)
LEFT JOIN tableD d ON (a.userid = d.userid)
LEFT JOIN tableE e ON (a.userid = e.userid)
WHERE d.userid IS NULL and e.userid IS NULL;
Related
I have so far written the following mysql query which is joining 4 tables as follows:
SELECT
T3.fault, t4.name
FROM table2 T2
INNER JOIN (
SELECT a.*
FROM table1 a
LEFT JOIN table1 b ON a.item_id = b.item_id AND a.submit_id < b.submit_id
WHERE b.submit_id IS NULL
) T1 ON T1.item_id = T2.item_id
INNER JOIN table3 T3 ON T1.id = T3.run_id
LEFT JOIN table4 T4
ON 3.runname_id = T4.id
order by count(*) desc;
Below is sample Data for table 2 which has item_id as PK
Example query: select * from table2 where item_id = '15907';
item_id host
15907 abc.com
7303 cde.com
7304 abcd.com
7305 cdedf.com
I have now recently added a table table5 which looks as below and has item_id as PK. I want to join table5 with table2 on item_id and want to retrieve value for restoreid also in my final query.
item_id restoreId
15907 12342
7303 12342
7304 14342
7305 14342
How to implement join between table5 and table2 on item_id? My select query should also retrieve T5.restoreId along with T3.fault, t4.name
select your_table.fault,your_table.name,t5.restoreid
from (
SELECT
T3.fault, t4.name,t2.item_id
FROM table2 T2
INNER JOIN (
SELECT a.*
FROM table1 a
LEFT JOIN table1 b ON a.item_id = b.item_id AND a.submit_id < b.submit_id
WHERE b.submit_id IS NULL
) T1 ON T1.item_id = T2.item_id
INNER JOIN table3 T3 ON T1.id = T3.run_id
LEFT JOIN table4 T4
ON 3.runname_id = T4.id
) as your_table left join table5 t5 on your_table.item_id = t5.item_id
SELECT
T3.fault, t4.name, T5.restoreid
FROM table2 T2
INNER JOIN (
SELECT a.*
FROM table1 a
LEFT JOIN table1 b ON a.item_id = b.item_id AND a.submit_id < b.submit_id
WHERE b.submit_id IS NULL
) T1 ON T1.item_id = T2.item_id
INNER JOIN table3 T3 ON T1.id = T3.run_id
LEFT JOIN table4 T4
ON T3.runname_id = T4.id
LEFT JOIN table5 ON T2.item_id = T5.item_id;
I am trying to execute the following query:
update table3 d set status = 'Complete'
where d.id in
(
select b.id from table1 a, table3 b, table2 c
where a.id = b.table1_id
and c.id = b.table2_id
and c.examId = 16637 -- will be passed in by user
and a.id in (46,47,48,49) -- will be passed in by user
);
So, I'm trying to update multiple rows of table3.
table3 is a join table between table1 and table2.
wrap it in a subquery, (thus creating a temporary table for the result). I'm also recommending to use ANSI SQL-92 format.
update table3 d
set status = 'Complete'
where d.id in
(
SELECT ID
FROM
(
select b.id
from table1 a
INNER JOIN table3 b
ON a.id = b.table1_id
INNER JOIN table2 c
ON c.id = b.table2_id
where c.examId = 16637 and
a.id in (46,47,48,49)
) xx
);
or by using JOIN
update table3 d
INNER JOIN
(
SELECT ID
FROM
(
select b.id
from table1 a
INNER JOIN table3 b
ON a.id = b.table1_id
INNER JOIN table2 c
ON c.id = b.table2_id
where c.examId = 16637 and
a.id in (46,47,48,49)
) xx
) y ON d.id = y.id
set status = 'Complete'
I have a table(T1) in which 2 columns(X and Y) are id's. The name's of these corresponding id's are in another table (T2) with the column name.
Suppose I was only using X then, a simple Inner join would have solved my problem in getting the name.
Such as
Select T1.somedata,T1.somedata1,T2.name from T1
Inner Join T2 ON T1.X=T2.id
But,what if I want the name's to be resolved for the T1.Y also?, which name would the Inner Join resolve it to ??
Select T1.somedata,T1.somedata1,T2.name from T1
Inner Join T2 ON T1.X=T2.id
Inner Join T2 ON T1.Y=T2.id
The above query is wrong, I know. Can I get the names of those corresponding to both T1.Xand T1.Y with an INNER Join?
-Beginner
I suggest always add aliases to tables and columns. So you will be sure which data are selected.
select
T1.somedata,
T1.somedata1,
T2X.name as XName,
T2Y.name as YName
from T1 as T1
inner join T2 as T2X on T2X.id = T1.X
inner join T2 as T2Y on T2Y.id = T1.Y
This selects both names as separate columns:
Select T1.somedata,T1.somedata1,T2a.name, T2b.name
from T1
Inner Join T2 as T2a ON T1.X=T2a.id
Inner Join T2 as T2b ON T1.Y=T2b.id
The following would generate two records in the result set:
Select T1.somedata, T1.somedata1, T2.name
from T1
Inner Join T2 ON T1.X=T2.id Or T1.Y=T2.id
you need to join table T2 twice and supply aliases on the names to avoid ambiguity.
SELECT a.*,
b.name as NameB,
c.name as NameC
FROM T1 a
INNER JOIN T2 b
ON a.x = b.id
INNER JOIN T2 c
On a.y = c.id
You need to join table T2 twice and supply aliases on the names to avoid ambiguity.
SELECT a.*,
b.name as NameB,
c.name as NameC
FROM T1 a
INNER JOIN T2 b
ON a.x = b.id
INNER JOIN T2 c
On a.y = c.id
SELECT table1.name, table2.wage, table2.bonus table3.shift, table3.endtime, table4.vacation
FROM table1
INNER JOIN table2 ON table1.userid = table2.userid
INNER JOIN table3 ON table2.userid = table3.userid
INNER JOIN table3 ON table2.endtime = table3.endtime
INNER JOIN table4 ON table4.userid = table4.userid
LEFT JOIN table5 ON table1.name = table5.position
this throws error: Not unique table/alias: 'table3'
i tried
INNER JOIN table3 tbl3 ON table2.userid = table3.userid
INNER JOIN tbl3 ON table2.endtime = tbl3.endtime
sill not working
Give it a unique alias:
SELECT table1.name, table2.wage, table2.bonus t3a.shift, t3b.endtime, table4.vacation
FROM table1
INNER JOIN table2 ON table1.userid = table2.userid
INNER JOIN table3 t3a ON table2.userid = t3a.userid
INNER JOIN table3 t3b ON table2.endtime = t3b.endtime
INNER JOIN table4 ON table4.userid = table4.userid
LEFT JOIN table5 ON table1.name = table5.position;
Actually, it makes for neater code to give every table an alias:
SELECT
t1.name,
t2.wage,
t2.bonus,
t3a.shift,
t3b.endtime,
t4.vacation
FROM table1 t1
INNER JOIN table2 t2 ON t1.userid = t2.userid
INNER JOIN table3 t3a ON t2.userid = t3a.userid
INNER JOIN table3 t3b ON t2.endtime = t3b.endtime
INNER JOIN table4 t4 ON t4.userid = t1.userid
LEFT JOIN table5 t5 ON t1.name = t5.position;
It looks like you have an error in your join for table4, and a missing comma in the selects - I have corrected it in this last version.
Use an alias with the keyword AS (which can be omitted).
INNER JOIN table3 AS t31 ON table2.userid = t31.userid
INNER JOIN table3 AS t32 ON table2.endtime = t32.endtime
Try aliasing the table names:
SELECT *
FROM Table1 t1
JOIN Table1 t2
ON t1.X = t2.Y
My query
SELECT table1.name, table2.wage, table2.bonus table3.shift, table4.vacation
FROM table1
INNER JOIN table2 ON table1.userid = table2.userid
INNER JOIN table3 ON table2.userid = table3.userid
INNER JOIN table4 ON table4.userid = table4.userid
LEFT JOIN table5 ON table1.name = table5.position
**LEFT JOIN vacation on vacation.userid = table1.userid
WHERE vacation.userid IS NULL**;
This selects all where userid is NOT inside vacation table.
Now i need to check if userid IS inside vacation table and if vacation.status = 1 for this userid should still show in query.
If this is a separate query/requirement, then the following query will work:
SELECT table1.name, table2.wage, table2.bonus table3.shift, table4.vacation
FROM table1
INNER JOIN table2 ON table1.userid = table2.userid
INNER JOIN table3 ON table2.userid = table3.userid
INNER JOIN table4 ON table4.userid = table4.userid
INNER JOIN vacation on vacation.userid = table1.userid
LEFT JOIN table5 ON table1.name = table5.position
WHERE vacation.status = 1;
If you need both requirements (userid is NULL or status = 1), try this:
SELECT table1.name, table2.wage, table2.bonus table3.shift, table4.vacation
FROM table1
INNER JOIN table2 ON table1.userid = table2.userid
INNER JOIN table3 ON table2.userid = table3.userid
INNER JOIN table4 ON table4.userid = table4.userid
LEFT JOIN table5 ON table1.name = table5.position
LEFT JOIN vacation on vacation.userid = table1.userid
WHERE vacation.user_id IS NULL or vacation.status = 1;