MySQL combining two queries into single query - mysql

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'

Related

MYSQL join not bringing correct result

I have created the following query which includes 2 joins and is working fine:
SELECT a.name, b.tid
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
WHERE b.status = 'FAIL';
The output is coming as follows:
id tid name
17695 19512 abc.com
17781 19628 abc1.com
17805 19732 abc2.com
17806 19703 abc3.com
17807 19704 abc4.com
I have another table table3 which has following values
id tid name details
842 19512 abc.com Details Description 1
843 19628 abc1.com Details Description 2
I want to join the above query with table3 on tid such that I get the following output
id tid name details
17695 19512 abc.com Details Description 1
17781 19628 abc1.com Details Description 1
I am using the following query but it is returning only one row which is not correct
SELECT a.name, b.tid
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
INNER JOIN table3 T3 on b.tid = T3.tid
WHERE b.status = 'FAIL'
Make your query a sub-query and join it to the table
WITH BASELINE AS
(
SELECT a.name AS name, b.tid AS tid
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
WHERE b.status = 'FAIL';
)
-------------------------------------------------------------------
SELECT
base.tid
, base.name
, t3.details
FROM
BASELINE base
LEFT JOIN
Table3 t3
ON base.tid = t3.tid
Is not the neatest solution but it always works for me

MYSQL Using Union to combine two queries

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 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
INNER JOIN table3 T3 on b.tid = T3.tid
WHERE b.status = 'FAIL'
AND b.type = 'Type1'
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 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
INNER JOIN table4 T4 on b.tid = T4.tid
WHERE b.status = 'FAIL'
AND b.type = 'Type2'
In my final result I want Output column which will have values either from T3.desc1 or T4.desc2
How can I use UNION to combine both queries into a single query?
just put UNION ALL between the queries. Youll have to alias the columns
Do this:
Query1 UNION Query2; -- for union without duplicates (the set union as in maths)
OR
Query1 UNION ALL Query2; -- for union with duplicates
So it should be:
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 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
INNER JOIN table3 T3 on b.tid = T3.tid
WHERE b.status = 'FAIL'
AND b.type = 'Type1'
UNION
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 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
INNER JOIN table4 T4 on b.tid = T4.tid
WHERE b.status = 'FAIL'
AND b.type = 'Type2';

Adding fifth join in the table

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;

Find common users in multiple tables in SQL

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;

can't specify target table for UPDATE in FROM clause

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'