I have a variable which is containing a dynamic query. Something like this:
$query = "SELECT id, subject FROM post1
UNION ALL
SELECT id, subject FROM post2
UNION ALL
SELECT id, subject FROM post3";
Also I have this query:
SELECT code FROM mytable WHERE id = :id
Now I want to join query above with that dynamic query. Here is my try:
SELECT t1.code t2.subject FROM mytable t1
LEFT JOIN ($query) t2 ON t1.col = t2.id
WHERE t1.id = :id
/*
SELECT t1.code t2.subject FROM mytable t1
LEFT JOIN (SELECT id, subject FROM post1
UNION ALL
SELECT id, subject FROM post2
UNION ALL
SELECT id, subject FROM post3) t2 ON t1.col = t2.id
WHERE t1.id = :id
*/
It works. But it takes a lot of time for huge data. How can I make it faster?
SELECT t1.code t2.subject FROM mytable t1
LEFT JOIN (SELECT id, subject FROM post1
JOIN mytable tt1 ON tt1.col = post1.id AND tt1.id=:id
UNION ALL
SELECT id, subject FROM post2
JOIN mytable tt2 ON tt2.col = post2.id AND tt2.id=:id
UNION ALL
SELECT id, subject FROM post3
JOIN mytable tt3 ON tt3.col = post3.id AND tt3.id=:id) t2 ON t1.col = t2.id
WHERE t1.id = :id
Just add the :id check to the internal querie4st to restrict amount of data selected.
You can use below query.
SELECT t1.code, t2.subject FROM (SELECT code, col FROM mytable WHERE id = :id ) t1
LEFT JOIN ($query) t2 ON t1.col = t2.id
Related
I searched around and found a near example to what I'm looking for, but it doesn't work in my case.
I have a query that does an INNER JOIN on two tables and this join constrains my overall data set substantially. I then want to LEFT JOIN onto a third table but I only want one record from that third table. The reason for the left join is because not every result of the INNER JOIN has a match in the 3rd table. Something like this:
SELECT DISTINCT t1.code, t2.id, t2.code, t3.id, t3.source_title, t3.display_order
FROM table1 t1
INNER JOIN table2 t2 ON t2.code=t1.code AND t2.type=0
LEFT JOIN table3 t3 ON t3.code=t1.code
ORDER BY t1.code, t3.display_order
This query returns too many records because the third table contains multiple records with a matching code. I just want the first one that matches with the lowest display_order value and, unfortunately, I can't limit the records to have display_order=1 because the lowest display order is not always one.
IMPORTANT: The t3.id value (if any) returned by this query must correspond to the record with the lowest display_order value. I.e., it won't work if the query correctly returns the lowest display_order value but the t3.id value corresponds to some other record in table 3.
Is this even possible? Any help would be much appreciated.
EDIT: Per Nick's suggestion, I have tried this, which appears to be working. I'll do some verification and report back:
SELECT DISTINCT t1.code, t2.*, sq.id, sq.source_title, sq.display_order
FROM table1 t1
INNER JOIN table2 p ON t2.code=t1.code AND t2.type=0
LEFT JOIN (
SELECT t3.*
FROM table3 t3
WHERE t3.display_order=(
SELECT MIN(display_order)
FROM table3 t3a
WHERE t3a.code = t3.code
)
) sq ON sq.code=t1.code
ORDER BY t1.code, sq.display_order
You should be able to replace table3 in your LEFT JOIN with
(SELECT *
FROM table3 t3
WHERE display_order = (SELECT MIN(display_order)
FROM table3 t3a
WHERE t3a.code = t3.code)
) t3
In MySQL 8.0 you can try to use row_number() for each code and ordered by display_order in a subquery from table3. Then left join that result and check for the row_number() to be equal to 1.
SELECT DISTINCT
t1.code,
t2.id,
t2.code,
t3.id,
t3.source_title,
t3.display_order
FROM table1 t1
INNER JOIN table2 t2
ON t2.code = t1.code
LEFT JOIN (SELECT t3.id,
t3.source_title,
t3.display_order,
t3.code,
row_number() OVER (PARTITION BY t3.code
ORDER BY t3.display_order) rn
FROM table3 t3) t3
ON t3.code = t1.code
WHERE t2.type = 0
AND t3.rn = 1
ORDER BY t1.code,
t3.display_order;
In lower versions you can try correlated subqueries ordered by display_order and LIMIT 1 (to get only one record).
SELECT DISTINCT
t1.code,
t2.id,
t2.code,
(SELECT t3.id
FROM table3 t3
WHERE t3.code = t1.code
ORDER BY t3.display_order,
t3.id
LIMIT 1) id,
(SELECT t3.source_title
FROM table3 t3
WHERE t3.code = t1.code
ORDER BY t3.display_order,
t3.id
LIMIT 1) source_title,
(SELECT t3.display_order
FROM table3 t3
WHERE t3.code = t1.code
ORDER BY t3.display_order,
t3.id
LIMIT 1) display_order
FROM table1 t1
INNER JOIN table2 t2
ON t2.code = t1.code
WHERE t2.type = 0
ORDER BY t1.code,
(SELECT t3.display_order
FROM table3 t3
WHERE t3.code = t1.code
ORDER BY t3.display_order,
t3.id
LIMIT 1);
I assumed, that display_order in table3 isn't unique but id is. So I added id to the ORDER BY clauses in the subqueries to make sure the same record is selected in each of them. If display_order is unique, you can remove id FROM the ORDER BY clauses.
Edit:
If you don't want to repeat the subqueries in the (overall) ORDER BY clause, you can also order by the column ordinals. E.g.:
...
ORDER BY 1, 6;
for a query like this
$query = "
SELECT t1.title, t1.content, t2.image, t2.tags
FROM blog1posts t1, blog1imagestags t2
WHERE id = :id
UNION ALL
SELECT t1.title, t1.content, t2.image, t2.tags
FROM blog2posts t1, blog2imagestags t2
WHERE id = :id
UNION ALL //THE PROBLEM
SELECT t1.title, t1.content, t2.tags
FROM blog2posts t1, blog3imagestags t2
WHERE id = :id
";
How can i UNION or JOIN the last part with the query?
You can try this (add a dummy field in third UNION):
SELECT t1.title, t1.content, t2.image, t2.tags
FROM blog1posts t1, blog1imagestags t2
WHERE id = :id
UNION ALL
SELECT t1.title, t1.content, t2.image, t2.tags
FROM blog2posts t1, blog2imagestags t2
WHERE id = :id
UNION ALL
SELECT t1.title, t1.content, '' AS image, t2.tags
FROM blog2posts t1, blog3imagestags t2
WHERE id = :id
How can I connect to 4 tables in a single query using forign key IDs?
I know how to connect to two tables.
$sql = "SELECT tb1.id, tb2.name FROM tblA tbl1 LEFT JOIN tblB tbl2 ON tb1.id = tbl2.studentID ORDER BY tbl1.id DESC LIMIT 20";
$statement = $con_db->prepare($sql);
Try like this :
select t1.ID, t2.studentID, t3.aID, t4.bID
from table1 as t1
left join tbl2 as t2 on t2.studentID = t1.id
left join tbl3 as t3 on t3.aID = t1.id
left join tbl4 as t4 on t4.bID = t1.id
I have this SQL query and I am getting an error.
SELECT
table1.id as id,
CONCAT( table2.first_name , ' ' , table2.last_name ) as name,
table2.country_code as country_code,
(select table3.id from table3 where table3.user_id = table1.id AND table3.description NOT LIKE '%SOMETEXTHERE%' LIMIT 1) as trans_id,
table4.important as important,
table1.status as status
FROM table1
LEFT JOIN table2 ON eid = table2.id
LEFT JOIN table4 ON table4.ewallet_transaction_id = trans_id
ORDER BY transaction_date desc;
The error is: Error Code: 1054. Unknown column 'trans_id' in 'on clause'
But the trans_id does exist.
Note: I also try other alias for 'trans_id' like 'transaction_id'
This might be an easy question but I just cant figure it out. Thanks for your help in advance.
Because your mix join syntax
To allow the join to be processed, group the first two tables explicitly with parentheses so that the operands for the ON clause are (t1,t2) and t3:
SELECT * FROM (t1, t2) JOIN t3 ON (t1.i1 = t3.i3);
Alternatively, avoid the use of the comma operator and use JOIN instead:
SELECT * FROM t1 JOIN t2 JOIN t3 ON (t1.i1 = t3.i3);
You have to use a JOIN FOR table3 too, this way
SELECT
table1.id as id,
CONCAT( table2.first_name , ' ' , table2.last_name ) as name,
table2.country_code as country_code,
table31.trans_id as trans_id,
table4.important as important,
table1.status as status
FROM table1
INNER JOIN (select id as trans_id, user_id from table3 where table3.description NOT LIKE '%SOMETEXTHERE%' GROUP BY 1) table31 ON table1.id=table31.user_id
LEFT JOIN table2 ON eid = table2.id
LEFT JOIN table4 ON table4.ewallet_transaction_id = table31.trans_id
ORDER BY transaction_date desc;
here us the query
SELECT *
FROM Table1
WHERE complete='Y'
AND shipped='Y'
AND active='Y'
AND create_dttm > '2013-10-10 08:28:41'
AND order_id IN
(SELECT DISTINCT t1.order_id
FROM Table2 t1
INNER JOIN table3 t2 ON t1.prod_id = t2.prod_id
WHERE t2.prod_sku LIKE '%D-600%'
AND t1.create_dttm > '2013-02-15 08:28:41')
You are using a sub-query in WHERE clause, that could be the main reason behind slow execution of your query. Try using JOINS instead of sub query.
SELECT t1.*
FROM Table1 t1
INNER JOIN Table2 t2 ON T1.order_id = T2.order_id
AND t2.create_dttm > '2013-02-15 08:28:41'
INNER JOIN table3 t3 ON t2.prod_id = t3.prod_id
AND t3.prod_sku LIKE '%D-600%'
WHERE complete='Y'
AND shipped='Y'
AND active='Y'
AND create_dttm > '2013-10-10 08:28:41'
And also check for indexes on your tables.