full outer join (sql) [duplicate] - mysql

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

Related

mysql join two tabes two columns not equal [duplicate]

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

Mysql query to retrive data from join of three table [duplicate]

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

Join 2 column same table with another column [duplicate]

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

Database Normalization: relationship based on join [duplicate]

This question already has answers here:
Which is more efficient: Multiple MySQL tables or one large table?
(9 answers)
Closed 5 years ago.
which is much better, multiple tables join with one main table, or each table join with one another.
example: 5 tables join with one main table
main table == 0
table 5 join table 0, table 4 join table 0, table 3 join table 0, table 2 join table 0, table 1 join table 0
each id's of 5 tables is in main table 0
OR
table 5 join table 4, table 4 join table 3, table 3 join table 2, table 2 join table 1, and table 1 join main table 0.
over all you cant say which one is the better version. The more you normalize, the more you have to join at some point it could lead into performance issues. If you need those joins often, i would prefere less normalization ;)

Mysql-foreign keys and array [duplicate]

This question already has answers here:
MySQL many-to-many relationship with FOREIGN KEYS
(3 answers)
Closed 7 years ago.
I have two tables. One is Category and the other one is Products. The problem is that one product can have more than one Category and I don't know how to do it.
I've related the tables with a foreign key 1:n.
Thanks
You'd need another table, maybe called ProductCategories.
Each row contains a foreign key to a product and a foreign key to a category.
When you want to find the categories for a product, query for all ProductCategories with that product ID.
You should create 3 table and related them together, something like this:
Table: Items
Columns: ID, Item_ID, Item_Title, Content
Table: Tags
Columns: Tag_ID, Tag_Title
Table: Items_Tags
Columns: Item_ID, Tag_ID
Item_ID is a foreign key in Items table.
Items_Tags is a correlation table.
And for example this code prints all x tags:
SELECT * FROM items i
LEFT JOIN item_tags it ON i.item_id = it.item_id
LEFT JOIN tags t ON t.tag_id = it.tag_id
WHERE tag_title = 'x'