I need to FULL OUTER JOIN multiple tables. I know how to FULL OUTER JOIN two tables from here. But I have several tables, and I can't apply it over them. How can I achieve it?
My SQL code, below:
INSERT INTO table
(
customer_id
,g01
,g02
,g03
,has_card
,activity
)
SELECT sgd.customer_id, sgd.g01,sgd.g02,sgd.g03,sc.value, a.activity
FROM s_geo_data sgd
LEFT JOIN s_category sc
ON sc.customer_id = sgd.customer_id
UNION
SELECT sgd.customer_id, sgd.g01,sgd.g02,sgd.g03,sc.value, a.activity
FROM s_geo_data sgd
RIGHT JOIN s_category sc
ON sc.customer_id = sgd.customer_id
UNION
SELECT sgd.customer_id, sgd.g01,sgd.g02,sgd.g03,sc.value, a.activity
FROM s_geo_data sgd
LEFT JOIN s_activity a
ON a.customer_id = sgd.customer_id
UNION
SELECT sgd.customer_id, sgd.g01,sgd.g02,sgd.g03,sc.value, a.activity
FROM s_geo_data sgd
RIGHT JOIN s_activity a
ON a.customer_id = sgd.customer_id
Also I tried this query:
INSERT INTO reportls.table
(
customer_id
,g01
,g02
,g03
,has_card
,activity
)
SELECT sgd.customer_id, sgd.g01,sgd.g02,sgd.g03,sc.value, a.activity
FROM s_geo_data sgd
LEFT JOIN s_category sc
ON sc.customer_id = sgd.customer_id
LEFT JOIN s_activity a
ON sc.customer_id = sgd.customer_id
UNION
SELECT sgd.customer_id, sgd.g01,sgd.g02,sgd.g03,sc.value, a.activity
FROM s_geo_data sgd
LEFT JOIN s_category sc
ON sc.customer_id = sgd.customer_id
RIGHT JOIN s_activity a
ON a.customer_id = sgd.customer_id
UNION
SELECT sgd.customer_id, sgd.g01,sgd.g02,sgd.g03,sc.value, a.activity
FROM s_geo_data sgd
RIGHT JOIN s_category sc
ON sc.customer_id = sgd.customer_id
LEFT JOIN s_activity a
ON a.customer_id = sgd.customer_id
Last query executes very long time, I need faster query.
I think to have a FULL OUTER JOIN over 3 tables, you need to do it like this:
SELECT t1.value, t2.value, t3.value
FROM t1 LEFT JOIN t2 ON t1.value = t2.value
LEFT JOIN t3 ON t1.value = t3.value
UNION ALL
SELECT t1.value, t2.value, t3.value
FROM t2 LEFT JOIN t1 ON t1.value = t2.value
LEFT JOIN t3 ON t2.value = t3.value
WHERE t1.value IS NULL
UNION ALL
SELECT t1.value, t2.value, t3.value
FROM t3 LEFT JOIN t1 ON t1.value = t3.value
LEFT JOIN t2 ON t2.value = t3.value
WHERE t1.value IS NULL AND t2.value IS NULL
As an alternative for this:
SELECT t1.value, t2.value, t3.value
FROM t1 FULL OUTER JOIN t2 ON t1.value = t2.value
FULL OUTER JOIN t3 ON t1.value = t3.value
I suggest you to create some temporary tables like t1, t2 and t3 for storing results of your queries, then use above query over those.
Related
My first query looks as below
SELECT a.name, b.desc, T3.desc1 as Output
FROM table1 a INNER JOIN
table2 b
ON a.id = b.id
INNER JOIN (
SELECT b2.id, b2.status, MAX(b2.created_time) as max_time
FROM q_scan b2
GROUP BY b2.id , b2.qualys_type
) t on t.id = a.id
AND t.status=b.status
AND t.max_time = b.created_time
INNER JOIN table3 T3 on b.tid = T3.tid
WHERE b.status = 'FAIL'
My second query looks as below
SELECT a.name, b.desc, T4.desc2 as Output
FROM table1 a INNER JOIN
table2 b
ON a.id = b.id
INNER JOIN (
SELECT b2.id, b2.status, MAX(b2.created_time) as max_time
FROM q_scan b2
GROUP BY b2.id , b2.qualys_type
) t on t.id = a.id
AND t.status=b.status
AND t.max_time = b.created_time
INNER JOIN table4 T4 on b.tid = T4.tid
WHERE b.status = 'FAIL'
In my final result I want Output column which will have values either from T3.desc1 or T4.desc2
How can I combine both queries into a single query?
If the table2.id exists in one of the tables - table3 or table4, a LEFT JOIN will simplify the query with the help of IFNULL().
SELECT a.name,
b.desc,
IFNULL(T3.desc1, T4.desc2) as Output
FROM table1 a
INNER JOIN table2 b ON a.id = b.id
INNER JOIN
(
SELECT b2.id, b2.status, MAX(b2.created_time) as max_time
FROM oac.qualys_scan b2
GROUP BY b2.id , b2.qualys_type
) t on t.id = a.id
AND t.status=b.status
AND t.max_time = b.created_time
LEFT JOIN table3 T3 on b.tid = T3.tid
LEFT JOIN table4 T4 on b.tid = T4.tid
WHERE b.status = 'FAIL'
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 run:
SELECT
t2.n, t3.n, t4.n,... tn.n
FROM t1
LEFT JOIN (SELECT ... FROM a LEFT JOIN b ON...) t2 ON t2.id = t1.c2
LEFT JOIN (SELECT ... FROM a LEFT JOIN b ON...) t3 ON t3.id = t1.c3
LEFT JOIN (SELECT ... FROM a LEFT JOIN b ON...) t4 ON t4.id = t1.c4
...
LEFT JOIN (SELECT ... FROM a LEFT JOIN b ON...) tn ON t4.id = t1.cn
All contents in bracket are the same:
SELECT ... FROM a LEFT JOIN b ON...
How can I call it 1 time instead of 'n' times as above?
t2.n, t3.n, t4.n,... tn.n have different values
I haven't try it you can try following code:
SELECT ... FROM t1
LEFT JOIN (SELECT ... FROM a LEFT JOIN b ON...) t2
ON
t2.id = t1.c2 OR
t2.id = t1.c3 OR
t2.id = t1.c4 OR
t2.id = t1.cn
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;
Is there a way to do a UNION between the LEFT JOIN lines, so that the results are not in a separate columns (lev1, lev2, lev3 and lev4), but in a single column (i.e. "ItemNo")?
Here's the MySQL query:
SELECT t1.ItemID AS lev1, t2.ItemID as lev2, t3.ItemID as lev3, t4.ItemID as lev4
FROM TableOfRelations AS t1
LEFT JOIN TableOfRelations AS t2 ON t2.ParentItemID = t1.ItemID
LEFT JOIN TableOfRelations AS t3 ON t3.ParentItemID = t2.ItemID
LEFT JOIN TableOfRelations AS t4 ON t4.ParentItemID = t3.ItemID
WHERE t1.ParentItemID = (SELECT ID FROM TableOfItems WHERE ItemID = 3599);
EDIT:
Here is a sample of result I am looking for:
ItemID ParentItemID JoinedDescription1 JoinedDescription2
3599 NULL MyString1A MyString1B
35 3599 MyString35A MyString35B
168 3599 MyString168A MyString168B
192 168 MyString192A MyString192B
238 3599 MyString238A MyString238B
266 168 MyString266A MyString266B
This result will be used for filling up a TreeView using VB.NET. Also the "JoinedDescriptions" are yet to be joined from a different table according to ItemID, but I think I can handle that easily once I get the basic table correctly.
Important note: Those selected lines are only belonging to one item (root item "3599" in this example), meaning it's only few lines out of thousands. Some recursion examples presume that all the table lines are used in the query, which is not my case.
No, there's not really a way to do a UNION between the LEFT JOIN operations.
But you can use the resultset from your query to get the result you want, by turning your query into an inline view.
Here's one way:
SELECT u.i AS lev
, CASE u.i
WHEN 1 THEN t.lev1
WHEN 2 THEN t.lev2
WHEN 3 THEN t.lev3
WHEN 4 THEN t.lev4
END AS levItemID
FROM ( SELECT 1 AS i
UNION ALL SELECT 2
UNION ALL SELECT 3
UNION ALL SELECT 4
) u
CROSS
JOIN ( SELECT t1.ItemID AS lev1
, t2.ItemID AS lev2
, t3.ItemID AS lev3
, t4.ItemID AS lev4
FROM TableOfItems t0
JOIN TableOfRelations t1
ON t1.ParentItemID = t0.id
LEFT
JOIN TableOfRelations t2
ON t2.ParentItemID = t1.ItemID
LEFT
JOIN TableOfRelations t3
ON t3.ParentItemID = t2.ItemID
LEFT
JOIN TableOfRelations t4
ON t4.ParentItemID = t3.ItemID
WHERE t0.ItemID = 3599
) t
Note: this is really more of a UNION ALL operation, it doesn't remove duplicates, it returns NULL values, and it returns the level of the parent.
You can tweak the query to get the results you want. If you don't care about the level number, remove the u.i from the SELECT list.
To remove duplicates, you can either add the DISTINCT keyword following SELECT, or add a GROUP BY clause. To eliminate NULL values, you can add a HAVING levItemID IS NOT NULL clause, etc.
I believe you are looking for concatenating strings.
See the Mysql Documentation on Concat:
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat
How about using a query as below.
select t1.ItemID as ItemNo
LEFT JOIN TableOfRelations AS t2 ON t2.ParentItemID = t1.ItemID
LEFT JOIN TableOfRelations AS t3 ON t3.ParentItemID = t2.ItemID
LEFT JOIN TableOfRelations AS t4 ON t4.ParentItemID = t3.ItemID
WHERE t1.ParentItemID = (SELECT ID FROM TableOfItems WHERE ItemID = 3599)
UNION
select t2.ItemID as ItemNo
LEFT JOIN TableOfRelations AS t2 ON t2.ParentItemID = t1.ItemID
LEFT JOIN TableOfRelations AS t3 ON t3.ParentItemID = t2.ItemID
LEFT JOIN TableOfRelations AS t4 ON t4.ParentItemID = t3.ItemID
WHERE t1.ParentItemID = (SELECT ID FROM TableOfItems WHERE ItemID = 3599)
UNION
select t3.ItemID as ItemNo
LEFT JOIN TableOfRelations AS t2 ON t2.ParentItemID = t1.ItemID
LEFT JOIN TableOfRelations AS t3 ON t3.ParentItemID = t2.ItemID
LEFT JOIN TableOfRelations AS t4 ON t4.ParentItemID = t3.ItemID
WHERE t1.ParentItemID = (SELECT ID FROM TableOfItems WHERE ItemID = 3599)
UNION
select t4.ItemID as ItemNo
LEFT JOIN TableOfRelations AS t2 ON t2.ParentItemID = t1.ItemID
LEFT JOIN TableOfRelations AS t3 ON t3.ParentItemID = t2.ItemID
LEFT JOIN TableOfRelations AS t4 ON t4.ParentItemID = t3.ItemID
WHERE t1.ParentItemID = (SELECT ID FROM TableOfItems WHERE ItemID = 3599)
Assuming you mean that you want the itemID on leaf nodes (I see no mention of itemNo in your query)....
SELECT
IFNULL(t4.ItemID, IFNULL(t3.ItemID, IFNULL(t2.ItemID, t1.ItemId))
(+remainder of your query)