Left join query - mysql

I have the following query
SELECT table1.*,
table2.status
FROM table1
LEFT JOIN table2 ON table1.transactionid = table2.transactionid
WHERE table2.transactionid IS NULL --Doesn't exist in table2
OR table2.status = 2
It returns me the table1 row if there isn't any row under the same transactionid in table2 or there is a row but it has status = 2 (error)
Now i need to modify it to don't return any table1 rows if there is a row with the same transactionid and status = 1 (completed) in table2
So, there can be multiple error rows but if there is one that is completed then i shouldn't get any result
Thank you

Not exists comes to mind:
SELECT t1.*
FROM table1 t1
WHERE NOT EXISTS (SELECT 1
FROM table2
WHERE t1.transactionid = t2.transactionid AND
t2.status = 1
);

Using 'NOT IN'
SELECT t1.*
FROM table1 t1
WHERE t1.transactionid NOT IN (SELECT t2.transactionid
FROM table2 t2
WHERE t1.transactionid = t2.transactionid AND
t2.status = 1
);
OR USING LEFT JOIN
SELECT t1.*
FROM table1 t1
LEFT JOIN table2 t2
ON t1.transactionid = t2.transactionid
WHERE t2.transactionid IS NULL

Related

Query Multiple IDs in 1 table row to another table in 1 query

I have 2 tables in MySQL
table1
ID|name|group_1|group_2|group_3
Table2
ID|group_name|Group_location
I want to get table 1 ID and show all the data and not tableIDs.
I'm trying to use a join like so
SELECT table1.name, table2.group_1, table1.group_2, table1.group_3
FROM tabl1 JOIN table2 on table1.group_1 = table2.ID AND
table1.group_2 = table2.ID AND table1.group_3 = table2.ID
WHERE table1.ID IN (868)
ORDER BY FIELD(table1.ID,868);
It's only returning the IDs in the result but I want it to return the table2 group_name
Is this what you want?
SELECT t1.name, t21.group_1, t22.group_2, t23.group_3
FROM table1 t1 LEFT JOIN
table2 t21
ON t1.group_1 = t21.ID LEFT JOIN
table2 t21
ON t1.group_2 = t22.ID LEFT JOIN
table2 t21
ON t1.group_3 = t23.ID
WHERE t1.ID IN (868)
ORDER BY FIELD(t1.ID, 868);
This uses LEFT JOIN in case any of the reference columns are NULL.
Try this;
SELECT t1.name
, t21.group_name
, t22.group_name
, t23.group_name
FROM table1 t1
LEFT
JOIN table2 t21
ON t1.group_1 = t21.ID
LEFT
JOIN table2 t21
ON t1.group_2 = t22.ID
LEFT
JOIN table2 t21
ON t1.group_3 = t23.ID
WHERE t1.ID IN (868)
ORDER
BY FIELD(t1.ID, 868);

SQL database : #1242 - Subquery returns more than 1 row

UPDATE table1 AS t1
INNER JOIN table2 AS t2 ON t1.table1_id=t2.table2_id
SET t1.overview=t2.val
WHERE t1.table1_id=(SELECT table2_id
FROM table2
WHERE table2_id=1);
table2 has multiple id values which are 1, so it gives
#1242 - Subquery returns more than 1 row
You need a DISTINCT.
UPDATE table1 AS t1
INNER JOIN table2 AS t2
ON t1.table1_id = t2.table2_id
SET t1.overview = t2.val
WHERE t1.table1_id = (SELECT DISTINCT table2_id FROM table2 WHERE table2_id = 1);
And if table2_id is fixed, why don't you just use 1 like:
UPDATE table1 AS t1
INNER JOIN table2 AS t2
ON t1.table1_id = t2.table2_id
SET t1.overview = t2.val
WHERE t1.table1_id = 1;
Obviously you have more than 1 record in table2 with same ID.
But if it's OK, change your = operator to in operator.
So This will be your code:
UPDATE table1 AS t1 INNER JOIN table2 AS t2 ON t1.table1_id=t2.table2_id SET t1.overview=t2.val where t1.table1_id in ( SELECT table2_id
FROM table2 WHERE table2_id=1);
Edit:
No need to subquery, it's redundant. Check this out:
UPDATE table1 AS t1 INNER JOIN table2 AS t2 ON t1.table1_id=t2.table2_id SET t1.overview=t2.val where table2_id=1;
If you indeed want to compare t1.table1_id against multiple values, use:
in instead of =
UPDATE table1 AS t1
INNER JOIN table2 AS t2 ON t1.table1_id=t2.table2_id
SET t1.overview=t2.val WHERE t1.table1_id
IN ( SELECT xxx FROM table2 WHERE table2_id=1);
BTW if you are only returning table2_id from the inner query, you can skip the inner query altogether.

case usage in mysqli for selecting and joining

Hello all I am having a simple problem while selecting some data from table and then joining the table with another table depending on the value of field in table 1
like i have table1, table2 and table 3.
I want to
select field1,field2 and then check the value of field 3 if field 3 has value = 1 then select field1,field2,field3 from table 2 and join table 1 with table 2 on field1 and field1 else select field1,field2,field3 from table3 and join table1 with table2 on field1 and field1.
I now this can be done case but i am not so comfurtable with it please help me solve the problem ..
I think this can be achieved using a UNION call:
SELECT t2.f1, t2.f2, t2.f3
FROM table2 t2
INNER JOIN table1 t1
ON t1.f1 = t2.f1
WHERE t2.f3 = 1
UNION ALL
SELECT t3.f1, t3.f2, t3.f3
FROM table3 t3
INNER JOIN table1 t1
ON t1.f1 = t3.f1
Try this:
SELECT t1.field1 AS t1field1,
t1.field2 AS t1field2,
CASE WHEN t1.field3 = 1 THEN t2.field1 ELSE t3.field1 END AS t2t3field1,
CASE WHEN t1.field3 = 1 THEN t2.field2 ELSE t3.field2 END AS t2t3field2,
CASE WHEN t1.field3 = 1 THEN t2.field3 ELSE t3.field3 END AS t2t3field3
FROM table1 t1
LEFT JOIN table2 t2 ON t1.field1 = t2.field1
LEFT JOIN table3 t3 ON t1.field1 = t3.field1;

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;

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
)