Using subquery in MySQL - mysql

I need some help with a specific problem about subqueries in MySQL. This is a shortversion of the problem I have:
I have table 1 and 2 where table 1 have a column called "SupplyID" which corresponds with table 2's "ID". I need to get a list with stuff from one single supplier.
I tried with this but didn't work:
select name
from item
where SupplyID exists (
select *
from SupplyID
where SupplyID = ID
);

Assuming your tables are named table1 and table2 you
You could use a inner join
select distinct t1.name
from ybale as t1
inner join table2 as t2 on t1.ID = t2.SupplyID

Try this:
select name from item i where exists (select * from table2 t where i.SupplyID = t.ID);

My answer is more like to what scaisEdge answered here but I strongly recommend to do this with LEFT JOIN. and If you want to select just one ID, for example ID=10
SELECT T1.name
FROM item AS T1
LEFT JOIN SupplyID AS T2 ON T2.SupplyID=T1.ID
WHERE T1.ID = 10

Related

SQL - show results from table without values from another table

I need to show only results which are in Table1 and Table2 but are not in Table3. Basically, it should be something like TABLE1, Table2 except INNER JOIN between (TABLE1, Table2) and TABLE3.
Should looks like this - On left side Table1 and Table2, on right side Table3
Now I have this:
SELECT mesta_email, mesta_kod
FROM Table1
UNION ALL
SELECT mesta_email, mesta_kod
FROM Table2
// And somehow except values which are in Table3
Can somebody help me please? Thanks a lot.
There are a couple different ways to do this. I believe mysql does better with the outer join/null approach:
select t.*
from (
SELECT mesta_email, mesta_kod
FROM Table1
UNION ALL
SELECT mesta_email, mesta_kod
FROM Table2
) t left join Table3 t3 on t.mesta_email = t3.mesta_email
and t.mesta_kod = t3.mesta_kod
where t3.mesta_email is null
This assume table3 shares the same structure as the other 2 tables.
I would approach the problem almost directly as you write it, using exists and not exists:
select t1.mesta_email, t2.mesta_kod
from table1 t1
where exists (select 1
from table2 t2
where t2.mesta_email = t1.mesta_email and t2.mesta_kod = t1.mesta_kod
) and
not exists (select 1
from table3 t3
where t3.mesta_email = t1.mesta_email and t3.mesta_kod = t1.mesta_kod
);
One advantage of exists/not exists over other approaches involves duplicates. If one of the tables (say table1) has not duplicates, but the others might, there is no need to remove duplicates in the resulting data set.

MySQL select count of another table's rows where the IDs match

I'm trying to select from one table with the count of another table where the id matches the original tableID sort of like:
select *, (count(0) from table2 where table2.table1ID = table1.table1ID) count
from table1
What's the mySQL syntax for this?
SELECT COUNT(b.*) FROM table1 as a
LEFT JOIN table2 as b ON a.tableID = b.tableID
select
table1.*,
if(table2.table1ID is null,0,count(*))
from
table1
left join table2 on table1.table1ID = table2.table1ID
group by
table1.table1ID;

How to combine 2 columns from 2 tables and subtract duplicates

I have 2 tables t1 and t2. Each have a customer ID column. What I am looking for is to join the 2 columns and SUBTRACT the duplicates.
My EG:
Table1 and Table2 with the IDs for each
I have tried a union query. The result I am left with is ID = 1,2,3,4,5,6,7,8,9,10. Where, what I'm after is subtracting 1-5 from Table2 and the result = 6,7,8,9,10.
I hope that makes sense and that someone is able to help. Sorry if this is a bit too simple compared to what you're all used to.
In SQL Server you can use the EXCEPT operator:
select ID
from Table2
except
select ID
from Table1
Mysql does not support it though. Using a an in clause or a left join would work in both servers:
--Using In clause
SELECT ID
FROM Table2
WHERE ID NOT IN
(
SELECT ID
FROM Table1
);
--Using join
SELECT Table2.ID
FROM Table2
left join Table1
on Table2.ID = Table1.ID
where Table1.ID is null
Use left outer join
select * from t1 left outer join t2 on t1.customerid = t2.customerid

SQL Select on multiple tables to check if value exists in one table and not used in other

I have 2 tables:
table1 (id,usedcode)
table2 (codeid,uniquecode)
I want to be able to check if a certain value exists in uniquecode of Table2, but is not already used in Table1
Try using left join as below:
SELECT t2.*
FROM table2 t2 LEFT JOIN table1 t1
ON t2.uniquecode = t1.usedcode
WHERE t1.usedcode IS null
SELECT uniquecode FROM Table2
WHERE NOT EXISTS(
SELECT * FROM Table1 WHERE usedcode = uniquecode
)
In English the query is saying, "Select all unique codes from table 2 that don't exist in table 1 as a usedcode".

mySQL outer join

I have 2 tables for which I need to run a query on
Table1 has 2 fields: l_id, and name
Table2 also has 2 fields: l_id, and b_id
I need to run a query to get the "name" and "l_id" for all the entries in table1 that do not have an entry in table2 for a given b_id.
Hope this makes some sense
select t1.*
from Table1 t1
left outer join Table2 t2 on t1.l_id = t2.l_id
and t2.b_id = #SomeValue
where t2.l_id is null
You can use an outer join, but I find a sub-query is a little more straightforward. In your case selecting everything from table1 that does not have an id in table2. Reads better...
SELECT * FROM table1 WHERE l_id NOT IN (SELECT l_id FROM table2);