This question already has answers here:
Return row only if value doesn't exist
(2 answers)
Closed 5 years ago.
For example table-A (id,number)
table-B(id,number)
now I want to delete Table-B records if id,number combination does not exist in table-A.
Can you any one help me on this?
sample data:
table-A
11,1001
12,1231
13,3451
table-B
11,3451
12,1231
54,1001
so i need to delete 11,3451; 54,1001 records from table-B
What about something like this to get the IDs that need to be deleted:
SELECT ID
FROM Table-B b
LEFT JOIN Table-A a on b.id = a.id AND b.number = a.number
WHERE a.id IS NULL
Related
This question already has answers here:
Why does using a subquery inside a left join give a completely different answer than an equivalent table?
(2 answers)
Access 2007 - Left Join to a query returns #Error instead of Null
(3 answers)
Closed 1 year ago.
I have two tables in Access:
Table1:
ID
1 A
2 B
3 C
Table2:
ID
1 A
2 B
3 D
and a query (Query1) that adds a new column to Table 1:
SELECT [Table1].ID
,"NEW CATEGORY" AS Category
FROM [Table1]
Which looks like this:
ID Category
1 A NEW CATEGORY
2 B NEW CATEGORY
3 C NEW CATEGORY
Now, if I join Query1 to Table2, like this:
SELECT [Table2].ID
,[Query1].ID
,[Query1].Category
FROM [Table2]
LEFT JOIN [Query1] ON ([Table2].ID = [Query1].ID)
I get the following result:
Table2.ID Query1.ID Category
1 A A NEW CATEGORY
2 B B NEW CATEGORY
3 D NEW CATEGORY
"NEW CATEGORY" magically appears in row #3 in the Category column, whereas I am expecting an empty cell there.
Any ideas of the source of this strange behavior, or how it can be avoided?
This is a different question because I have joined 2 tables. The solutions for the duplicate question indicated does not work for me.
This is the query:
SELECT a.id, a.userName, IF(o.userId = 1, 'C', IF(i.userId = 1,'I','N')) AS relation
FROM tbl_users AS a
LEFT JOIN tbl_contacts AS o ON a.id = o.contactId
LEFT JOIN tbl_invites AS i on a.id = i.invitedId
ORDER BY relation
This returns the output as follows:
id username relation
1 ray C
2 john I
1 ray N
I need to remove the third row from the select query by checking if possible that id is duplicate. I tried adding distinct(a.id) but it doesn't work. How do I do this?
This question already has answers here:
MySQL pivot row into dynamic number of columns
(1 answer)
MySQL - Rows to Columns
(13 answers)
Closed 5 years ago.
I have tree tables:
Persons:
id
name
Books
id
title
Quantity:
People_id
Product_id
quantity
I need a result with :
in the columns the Book title, in the rows the Persons name, in the cells the quantity take from the cross of peoples and books
select *
from persons p join quantity q on p.id = q.people_id
join books b on q.product_id = b.id
JOIN should help (if you need columns from all table then add alias.* for other tables in SELECT).
SELECT p.*
FROM persons p
JOIN quantity q ON p.id = q.people_id
JOIN books b ON q.product_id = b.id
This question already has answers here:
How to join two columns to the same table [duplicate]
(3 answers)
Closed 5 years ago.
I want to join 2 column that share same foreign key in another table
here's the tables:
country:
idcountry| countryname
1 german
2 america
destination
id|fromcountry |tocountry
1 1 2
the result that i wanted to:
id|fromc |toc
1 german america
Use simple Left Join ans provide alias to country name column:
SELECT d.idcountry as id, cf.CountryName as fromc, ct.CountryName as toc
FROM destination d
LEFT JOIN country cf ON d.fromcountry = cf.idcountry
LEFT JOIN country ct ON d.tocountry = ct.idcountry
Use Left Outer Join for both fields
something like this
SELECT Dest.ID, CFrom.CountryName, CTo.CountryName
FROM Destination Dest
LEFT OUTER JOIN Country CFrom ON Dest.FromCountry = CFrom.idcountry
LEFT OUTER JOIN Country CTo ON Dest.ToCountry = CTo.idcountry
This question already has answers here:
How can I do a FULL OUTER JOIN in MySQL?
(15 answers)
Closed 8 years ago.
I have 3 tables Category,subcategory and product
how can I do full outer join between these tables but on version 5.1.69 (mysql)
[category.cat_id] => category table l
[subcategory.subcat_id] => subcategory tab1e 2
[product.subcat_id] => product table 3
Have cat_id as a foreign key in Subcategory table and subcat_id as a foreign key in product table.
here is an example inner join:
Select product_name,cat_name,subcat_name
from Product p,Category c,Subcategory s
where c.cat_id=s.cat_id and p.subcat_id=s.subcat_id and product_id=121