Avoiding Multiple Joins to optimize the query in MYSQL - mysql

I have got a situation where I need to join 7 tables .
In this some tables have less records but Table1,Table2 and Table7 have huge records.
SELECT T2.id,
T2.name,
------,
------,
T7.name
FROM table1 T1 # Table1 have 1 million Records
INNER JOIN table2 T2 # Table2 have half million Records
ON T1.id = T2.id
INNER JOIN table2 T3
ON T2.id = T3.id
INNER JOIN table2 T4
ON T3.id = T4.id
--------------
---------------
---------------
INNER JOIN table7 T7 # Table7 have 1 million Records
ON T1.id = T7.id
I tried to filter these records load those in Temp Table
CREATE temporary TABLE Filter_Records_tmp
SELECT T.id,
T.name
FROM table2 T
WHERE EXISTS (SELECT id
FROM table1
WHERE id = T.id)
So that I can use Filtered records in Joining in place of Table1 and Table2.
But my question is same table1 I need to join with Table7.
How can I proceed on this ?
Any Suggestions.....

Related

Selecting from multiple tables with LEFT JOIN

I have 3 tables
t1 (select these records)
-------------
id
offer_id
business_id
t2 (offer details)
-------------
id
offer_details
business_id
t3 (business details)
-------------
id
business_name
I need to select all records from t1 and add information from t2 and t3. Seems basic but I can't seem to be able to get it right -- must be the heat.
SELECT t2.offer_details, t3.business_name
FROM t2
LEFT JOIN t1 ON (t1.offer_id = t2.id)
LEFT JOIN t3 ON (t1.business_id = t3.id)
should be
SELECT t2.offer_details, t3.business_name
FROM t1
LEFT JOIN t1 ON (t1.offer_id = t2.id)
LEFT JOIN t3 ON (t1.business_id = t3.id)
Your lead table is t1 and the join should be based on this table
How about this
Select t2.offer_details, t3.business_name
From t1
Left Join t2 ON (t1.offer_id = t2.id)
Left Join t3 ON (t1.business_id = t3.id)
If you want all records from t1, add t1.* on your select part. Assuming that all IDs in t1 exists in the other 2 tables
SELECT
t1.*, t2.offer_details, t3.business_name
FROM
t1
JOIN t2 ON t2.id = t1.offer_id
JOIN t3 ON t3.id = t1.business_id
Modify to LEFT JOIN if the IDs in t1 may be missing in t2 or t3.

mysql foreign key foriegn key relationship

Say, myself having three MySQL tables as follows.
Table 1 contains table1id, value columns.
Table 2 contains table2id, value, table1id (as FK) columns.
Table 3 contains table3id, value, table1id (as FK) columns.
Then is the following relationship valid?
select * from table1 t1 inner join table2 t2 on t1.table1id = t2.table1id
Yes, It is possible.
This one is Joining for teable1 and table2
select * from test1 t1 inner join test2 t2 on t1.id = t2.id;
This one is joining all three tables,
SELECT * FROM test1 t1
INNER JOIN test2 t2 ON t1.id = t2.id
INNER JOIN test3 t3 ON t1.id = t3.id;
Output: ONLINE DEMO HERE
Try this
SELECT * FROM
table1 t1 INNER JOIN table2 t2
ON t1.table1id = t2.table2id
INNER JOIN table3 t3
ON t1.table1id = t3.table3id
You can also write it this way:
SELECT * FROM table1 t1, table2 t2, table3 t3
WHERE (t1.table1id=t2.table2id) AND (t1.table1id=t3.table3id);
** If you want to join only the first 2 tables--Use the code until the AND
*** If you want to join all the tables-- Use the entire code.

How to LEFT JOIN table1 ON table2 WHERE table2 row fulfills certain conditions

Please consider this query:
SELECT table1.* ,
(SELECT quantity FROM table2 WHERE id = table1.id AND table2.location = 10) quantity,
(SELECT reorder_level FROM table2 WHERE id = table1.id AND table2.location = 10) reorder_level,
(SELECT stock_date FROM table2 WHERE id = table1.id AND table2.location = 10) stock_date
FROM table1
WHERE category_id = 5 ORDER BY table1.id;
The aliases quantity, location and stock_date are obviously referencing a a row in table2 that fulfill the condition: id=table1.id and location=10.
This query works, but is probably suboptimal as a result of the clumsy subqueries.
How can I best join table1 to table2 USING(id) but only on rows where location is also 10.
TIP: One row from table1 has many rows in table2.
Unfortunately, the actual table definitions are much more complex, and I reckoned it might be counter-productive to dump the entire thing on this thread.
You can use additional condition in ON() part so it will join only rows which fulfills the provided criteria
SELECT t1.* ,
t2.quantity ,
t2.reorder_level,
t2.stock_date
FROM table1 t1
LEFT JOIN table2 t2 ON t1.id = t2.id AND t2.location = 10
WHERE t1.category_id = 5
ORDER BY t1.id;
Another way would be use a subselect for your table2 and select only rows where location is equal to 10
SELECT t1.* ,
t2.quantity ,
t2.reorder_level,
t2.stock_date
FROM table1 t1
LEFT JOIN
(SELECT * FROM table2 WHERE t2.location = 10) t2
ON t1.id = t2.id
WHERE t1.category_id = 5
ORDER BY t1.id;

SQL trouble with COUNT

I have some SQL code that returns me some data from DB
SELECT t1.id as id, title, description FROM table1 t1
JOIN table2 t2 ON t1.id = t2.t1_id
WHERE t2.t3_id IN( SELECT id FROM table3 WHERE parent_id IN ( SELECT id FROM table3 WHERE parent_id = 1)) GROUP BY t1.id
I have some problem with counting number of rows of result. I know that I have to write almost the same code but with COUNT but I have there A problem, my code doesn't return me a number of rows.
Just use the COUNT(*) function. Also, your subqueries can be converted to a JOIN (and your sub-subquery is redundant):
SELECT COUNT(*)
FROM table1 t1
JOIN table2 t2
ON t1.id = t2.t1_id
JOIN table3 t2
ON t3.id = t2.t3_id
WHERE t3.parent_id = 1

MySQL Join and inner selects

I currently have a query
SELECT id FROM table1 WHERE {filters on table1} AND id NOT IN (SELECT table1ID FROM table2 WHERE condition = 0)
Table1 has a 1 - Many relationship with table2 and I'm looking for all the IDs that have no entries in table2 with condition=0.
Is there any way to rewrite this query without the inner select? I'm been scratching my head about it for a while now and any pointers would be welcome.
You can try something like
SELECT id
FROM table1 t1 LEFT JOIN
table2 t2 ON t1.ID = t2.table1ID
AND t2.Condition = 0
WHERE {filters on table1}
AND t2.table1ID IS NULL
Or just as good would be
SELECT id
FROM table1 t1
WHERE {filters on table1}
AND NOT EXISTS (
SELECT 1
FROM table2 t2
WHERE t1.ID = t2.table1ID
ADN t2.condition = 0
)