left join sorting with NULL values - mysql

i need to sort left join by company id so that categories selected by companies will appear first.
My category table is
company_category table is
expected result should be

I am supposing below query is best solution rather than using union to show company categories on top.
SELECT * FROM category c
LEFT JOIN company_category cc ON c.category_id=cc.category_id AND cc.company_id=1
ORDER BY cc.company_id DESC

Related

Keeping Records That Are Not Joined MYSQL

I have the following query in my application:
SELECT
p.old_product_id,
l.product_id,
p.sku,
p.title,
p.option_one,
p.option_two,
FROM
lookup_id l
JOIN temp_price_tables_data p USING (sku);
And it works great. However, a small percentage of records from the temp_price_tables_data tables don't make it to the results.
This is because the skus from the lookup_id table don't exist in the temp_price_tables_data.
Is there a way to keep these records in the new data?
Or is there a way to only get those records so I can store the result for later processing?
EDIT:
First table columns = old_product_id, sku, title, option_one, option_two
Second table column = product_id, sku
Tables should have SKU in common.
Use a left outer join:
SELECT
*
FROM
lookup_id l
LEFT OUTER JOIN price_tables_data p on l.sku = p.sku
WHERE old_product_id IS NULL;
That will get you all the records that are in temp_price_tables_data but not in lookup_id

mysql join on product category multiple relation query

I have three tables as below, each product may belong to multiple categories.
PRODUCT TABLE AS P
1. ID
2. NAME
CATEGORY TABLE AS C
1. ID
2. NAME
RELATION TABLE AS R
1. ID
2. P_ID
3. C_ID
Now I want to get a list of all products in product table, and with their belonging category name display as well.
How do I write this query?
I can get the category ids pulled from the same query, but don't know how to get all the name pulled as well. Here's what I tried.
select p.*,y.*
from p
left join (select p_id,group_concat(c_id) as category_ids
from relation group by p_id) as y on p.id=y.p_id
Do both JOIN operations (to the relation table, and from there to the table containing the category names) and feed the result to your aggregation function (GROUP_CONCAT)
SELECT P.Name, GROUP_CONCAT(DISTINCT C.Name ORDER BY C.Name SEPARATOR '|') categories
FROM Product P
LEFT JOIN Relation R ON P.ID = R.P_ID
LEFT JOIN Category C ON R.C_ID = C.ID
GROUP BY P.ID, P.Name
This will give you one row per product with the categories separated by |.
This uses LEFT JOIN operations so it won't suppress products that have no categories.
Select P.Name, C.Name
From RELATION R Inner Join PRODUCT P
On R.P_ID=P.Id
Inner Join Category C
On C.Id=R.C_ID
This query will get you all the products, with their corresponding category.
I want to give you a small explanation about the difference between Inner Join and Left Join.
If we take as an example 2 tables :
TA(idA, description) and TB(idB, idA, description).
Select TA.description, TB.description
From TA Inner Join TB On TA.IdA = TB.IdA
will get only the rows in TA, that have a corresponding one in TB.
On the other side,
Select TA.description, TB.description
From TA Left Join TB On TA.IdA = TB.IdA
will get all the rows of TA and if the row in TA doesn't have a corresponding one in TB, TB.description for this row will be NULL.
Hope this helps!

How to left join to select with statement

I have some tables:
products
--------------------------
|p_id|col|col...|...|cat_id|
colors
-----------
|id|p_id|url|
I want select apropriate rows from first table after join to this result another column selected by apropiate id.
In other words I select all products on some cathegory and how many colors each product has.
For example
select *
from products p
where p.p_id = some_number
and join to this
select count(*)
from colors c
where c.p_id = p.p_id
Thanks in advance
Left join return all row of left table(products) even row's id are not match of right table. for more info click here
SELECT p.p_id, COUNT(*) AS colours_count
FROM products AS p
LEFT JOIN colors c
ON c.p_id=p.p_id
GROUP BY p.p_id
Try this:
select p.p_id, count(*) as number_of_colours
from products as p
left join colors c on c.p_id=p.p_id
group by p.p_id
The LEFT JOIN operation guarantees that all rows of table products will be returned. Grouping by p_id, we get a separate row for each product along with the number of colours each product is associated to.
You can do the LEFT JOIN, but note that this returns a row from the left hand table irrespective of whether there is a matching row on the right hand table. Hence it there is no match or 1 match you will still get 1 row returned. As such if you use COUNT(*) you will get a count of 1.
So get around this you count the occurrences of a field from the right hand table (normally the primary key). Used like this COUNT() returns the count of non null fields, so it will return 0 when there is no matching row on the right:-
SELECT p.p_id,
COUNT(c.id) AS number_of_colours
FROM products p
LEFT OUTER JOIN colors c ON c.p_id = p.p_id
GROUP BY p.p_id

What is the difference between these two MySQL queries?

SELECT COUNT( companyId )
FROM Companies
LEFT JOIN Cities ON Cities.cityId = Companies.cityId
GROUP BY Companies.companyId;
VS
SELECT COUNT( companyId )
FROM Cities
LEFT JOIN Companies ON Cities.cityId = Companies.cityId
GROUP BY Companies.companyId;
What is the difference?
In the first query left table is Companies and in the second query Cities.
The LEFT JOIN keyword returns all rows from the left table
(table_name1), even if there are no matches in the right table
(table_name2).
FIRST QUERY
The LEFT JOIN keyword returns all rows from the Companies table
, even if there are no matches in the Cities table
SECOND QUERY
The LEFT JOIN keyword returns all rows from the Cities table
, even if there are no matches in the Companies table
Visual Representation of SQL Joins
I'm just placing a picture (self explained)
MySQL left JOIN

how can I select from one table based on non matches in another in mySQL

I have two tables
ITEMS : id, name
CATEGORIES: items_id, category
I need to select all the items whose IDs are NOT in the CATEGORIES table.
I suspect it's really simple, but can't figure out the syntax.
try this:
SELECT
i.*
FROM Items i
LEFT OUTER JOIN Categories c ON i.id=c.items_id
WHERE c.items_id is NULL
NOT IN (select CATEGORIES.item_id)
not sure if that's faster than the join above... but it works.
SELECT * FROM Items
WHERE id NOT IN (SELECT items_id FROM Categories)
This selects everything from the items table and only the records from categories that match the join. Then filter out the nulls.
Select Item.Id
FROM ITEMS LEFT OUTER JOIN CATEGORIES On
Items.Id = Categories.items_id
where categories.items_id is null
How about
SELECT id
, name
FROM ITEMS
WHERE NOT EXISTS(SELECT 1 FROM CATEGORIES WHERE Categories.items.id = ITEMS.id)
This will only bring back items that do not have at least one entry in the Categories table
SELECT items.id
FROM items
WHERE NOT EXISTS( SELECT *
FROM categories
WHERE items.id = categories.items.id )
This is the same as joining to the categories table as Mike Pone and KM listed but I find this more readable.