I have 2 tables in a MySQL DB:
Table 1 : id, galleryname
Table 2 : galleryid, <many other fields...>
Using PHP, I need to select all rows in Table 1 based on its ID where that id (galleryid) does not appear in Table 2.
Example:
Table 1
1, flowers
2, water
3, mountains
4, winter
Table 2
3, ...
would return these rows from Table 1
1, flowers
2, water
4, winter
I'm not exactly sure how to go about this. I am pretty good at the basics of MySQL but I suspect this is a JOIN or a UNION that is out of my league.
Any help is appreciated.
Try this:
SELECT * FROM table1
WHERE id NOT IN
(SELECT galleryid FROM table2)
or
SELECT * FROM table1
LEFT JOIN table2
ON table1.id = table2.galleryid
WHERE table2.galleryid IS NULL
Left join brings all the t1 records, then filter out those that have t2.galleryid NULL (no records in t2)
SELECT id, galleryname
FROM table1 AS t1
LEFT OUTER JOIN table2 AS t2 ON t1.id = t2.galleryid
WHERE t2.galleryid IS NULL
SELECT * FROM table1
LEFT JOIN table2
ON table1.id = table2.galleryid
WHERE table2.galleryid IS NULL
Someone posted an answer (then deleted it) that gave me ONLY the record that was in both. All others here seemed to give errors but using that original post I made one change and it worked.
Original:
SELECT * FROM table1 INNER JOIN table2 ON galleries.id = images.galleryid
(this gave me just the one that was in both)
Adding the !:
SELECT * FROM table1 INNER JOIN table2 ON galleries.id != images.galleryid
(this gave me what I needed)
Related
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
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
I have been trying to find the correct query for this problem but it doesn't quite work so I'm asking here:
I have 2 tables:
Table-1 has 5 rows with 6 attributes each
Table-2 has 3 rows with 5 attributes and EACH attribute corresponds to 1 row from table 1. What I want is a query that will give me all the attributes from table 1 that are contained in table 2. I've come up to this:
SELECT *
FROM Table1
WHERE PrimKey IN
(SELECT *
FROM Table2
WHERE PrimKey = Index)
However it won't let me do this because it says that on the second SELECT I can't select all but I have to choose. This way I can only view 1 row of Table1 stuff from Table2 but I want to view all of Table2's attributes.
SELECT *
FROM Table1
WHERE PrimKey IN
(SELECT PrimKey
FROM Table2)
Or INNER JOIN
SELECT t1.*
FROM Table1 t1 INNER JOIN Table2 t2
ON t1.ReferencingColumn = t2.ReferencingColumn
I'm desperate with this query. I have two tables table1 and table2, tables are identical but they have different data. I'm trying to remove duplicities by columns code and manufacturer. To do that I need in final result ID from table1 ID from table2 and also columns code and manufacturer
SELECT * FROM (
SELECT id,code,manufacturer FROM table1 WHERE manufacturer = 1
UNION SELECT id,code,manufacturer FROM table2 WHERE manufacturer = 1
) AS t GROUP BY code HAVING COUNT(*) > 1
But in result i got only values from table1. It's OK but I just need to get there id from table2 too. Please can anyone give me some tips how to do this ?
You have two basic problems:
Problem 1:
You are using UNION when you should be using UNION ALL, because UNION removes duplicates!
Problem 2:
This isn't the right way to go about the problem. You should be using a simple join, not a union.
Try this:
SELECT
t1.id as table1_id,
t2.id as table2_id,
t1.code,
t1.manufacturer
FROM table1 t1
JOIN table2 t2
ON t2.code = t1.code
AND t2.manufacturer = t1.manufacturer
WHERE manufacturer = 1 -- this WHERE clause is optional
Your use of the WHERE clause is a little odd - consider removing it to get all duplicates from all manufacturers.
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);