i have two tables :
Product :
id name category
1 AAA BBB
2 CCC DDD
3 EEE FFF
Ordre:
id id_product date
1 2 10/11/16
2 2 06/16/16
3 3 12/09/16
4 1 02/06/16
5 3 15/10/16
in order to know if a product has an order i create this select query :
SELECT id,name ,category
CASE WHEN id IN (select id_product from Ordre) then 'Y'
ELSE 'N' END AS has_ordre
FROM product;
but this is not working for me, in the fact i want to create a View that contains the info about the products and also has the column "has_ordre" to check if a product has an ordre or not.
do you have any suggestion?
i'm new in Mysql
thanks in advance.
You can do what you need without a sub-query by doing a join on the two tables and a COUNT() on order.id_product, which will count the number of orders for each product. NOTE: this query will completely (as designed) exclude products which have no orders. Also, the result for this particular query will show the number of orders for each product:
SELECT p.id, p.name, p.category, COUNT(o.id_product) AS num_orders
FROM product p
RIGHT JOIN order o
ON p.id = o.id_product
GROUP BY p.id
ORDER BY num_orders
You can change the RIGHT JOIN to a LEFT JOIN if you would like to display all orders regardless of whether or not they have an active order:
Here is a fiddle
Related
I have a somewhat far-fetched question I want to know if there is any way to solve it:
I have a table called PRODUCTS with these attributes:
ID, NAME, DATE
I have a table called PRODUCTS_STATUS with these attributes:
ID, PRODUCT_ID, STATUS_ID, DATE
I have a table called STATES with these attributes:
ID, STATUS_NAME
Now, as you have seen, it will be about:
STATES_PRODUCTS will have many STATES of PRODUCTS and STATUS will be many times in PRODUCT_STATUS
This means that PRODUCT_STATUS has a many-to-many relationship between states and products, since a PRODUCT will go through different state transitions.
Perfect up to there, it turns out that I have this case suppose:
PRODUCTS_STATES
ID, PRODUCT_ID, STATUS_ID, DATE
1 1 1 00:00:00
2 2 1 00:00:00
3 3 1 00:00:00
4 1 5 00:00:00
I am doing a query at the moment between PRODUCTS and PRODUCT_STATUS, at first I made a query where I told him to show me all the PRODUCTS that have in PRODUCT_STATUS the STATUS_ID= 1
But if you realize for example the PRODUCT_ID is twice in the table PRODUCT_STATUS with the STATUS_ID = 1 and 5
Ask:
Is it possible to make a query where it shows me all PRODUCTS with PRODUCT_STATUS equal to 1 but where PRODUCTS_ID that already has another state are excluded?
that is, for this example, I want to know if we could see PRODUCTS 2 and 3 because if they see 1 it already has another state.
Notice I have this query made but it is not useful for what I want:
SELECT
*
FROM
products
inner join product_status in products.id = product_status.product_id
WHERE
product_status.status_id not IN (
SELECT
product_status.id_status
FROM
product_status
WHERE
product_status.id_status = 5
)
Obviously they will realize that the query returns all the products with STATUS_ID = 1 but I no longer want to see PRODUCT 1 because I am no longer interested.
I would like to exclude all those PRODUCTS that have 1 and 5.
I hope you can give me an idea of how it is done. Thanks.
I recommend NOT EXISTS instead of NOT IN, because NOT IN has strange behavior when working with NULL values (and EXISTS often optimizes better than IN).
But your version is quite close:
SELECT p.*
FROM products p JOIN
product_status ps
ON p.id = ps.product_id
WHERE ps.status_id = 1 AND
NOT EXISTS (SELECT 1
FROM product_status ps2
WHERE ps2.product_id = ps.product_id AND
ps2.status_id <> 1
);
Note that this introduces tables aliases (abbreviations for the tables). This makes the query easier to write and to read.
Even though my question was warned as similar title, I couldn't find here any similar problem. Let me explain in details:
I've got two tables (I'm working with MySQL) with these values inserted:
table products:
id name
1 TV
2 RADIO
3 COMPUTER
table sales (product_id is A FK which references products(id)):
id quantity product_id
1 50 2
2 100 3
3 200 3
The tv's haven't been sold, radios got 1 sale (of 50 unities) and computers got two sales (one of 100 e other of 200 unities);
Now I must create a query where I can show the products and its sales, but there are some conditions that make that task difficult:
1 - If there's no sales, show obviously NULL;
2 - If there's 1 sale, show that sale;
3 - If there's more than 1 sale, show the latest sale (which I've tried to use function MAX(id) to make it simple, and yet didn't worked);
In the tables example above, I expect to show this, after a proper SQL Query:
products.NAME sales.QUANTITY
TV NULL
RADIO 50
COMPUTER 200
I've been trying lots of joins, inner joins, etc., but couldn't find the result I expect. Which SQL query can give the answer I expect?
Any help will be very appreciated.
Thanks.
Hope the below query works.
SELECT products.name, sl.quantity
FROM products LEFT JOIN (
SELECT product_id, max(quantity) as quantity FROM sales GROUP BY product_id) sl
ON products.id = sl.product_id
In MySQL 8.0 you can do:
with m (product_id, max_id) as ( -- This is a CTE
select product_id, max(id) from sales group by product_id
)
select
p.name,
s.quantity
from products p
left join m on m.product_id = p.id
left join sales s on s.id = m.max_id
If you have an older MySQL, you can use a Table Expression:
select
p.name,
s.quantity
from products p
left join ( -- This is a table expression
select product_id, max(id) as max_id from sales group by product_id
) m on m.product_id = p.id
left join sales s on s.id = m.max_id
So I have two tables:
products
id
name
product_variants
id
product_id
name
barcode
I want to select all products with one query containing one field with the amount of related variants and one field with all related barcodes (seperated by space).
So for example this output:
product_id product_name product_variant_count product_variant_barcodes
1 Product 1 3 1234567890 0987654321 5432109876
2 Product 2 1 6789054321
3 Product 3 2 1234509876 3456781290
Is this possible?
As mentioned in the comments GROUP_CONCAT is perfect for this.
Selecting from products and joining onto product_variants:
SELECT p.id, p.name, COUNT(pr.id) AS product_variant_count,
GROUP_CONCAT(pr.barcode SEPARATOR ' ') AS product_variant_barcodes
FROM products p
LEFT JOIN product_variants pr ON (p.id = pr.product_id)
GROUP BY p.id
Suppose I have a Product table, and a
id product
1 Apple
2 Bag
3 Cat
4 Ducati
and a Cart table
id user_id product_id
1 1 2
2 1 3
3 2 1
4 3 1
So, I want to look at a particular user and see what he/she does NOT have in their Cart.
In other words, in the above example
SELECT ...... WHERE user_id=1 .....
would return Apple and Ducati because User 1 already has Bag and Cat.
(This may well duplicate another question but there are so many variations I couldn't find the exact match and put in these simple terms may help)
Perform a left join from product to all products purchased by user1, which can be retrieved with a subselect in the join. This will cause all product id's that are not in user1's care to have null product ids. The where clause will select all null product id's meaning they will not have been in a users cart, essentially filtering purchased items.
select p.name
from product p
left join (select product_id, user_id
from cart where user_id = 1)
c
on p.id = c.product_id
where c.product_id is null;
SQL Fiddle: http://sqlfiddle.com/#!2/5318eb/17
Select
*
From Product p
Where p.id Not In
(
Select c.product_id
From Cart c
Where User ID = ____
)
SELECT product FROM product_table
WHERE product NOT IN
(SELECT product_id FROM cart_table WHERE user_id = 1);
This will give you all product for all users which are not in there cart.
select c.user_id,a.Product
from cart c Cross Join product a
left Join
cart b on b.product_id=a.id and c.user_id=b.user_Id
where b.product_id is null
group by c.user_id,a.Product
Sql Fiddle Demo
I have 2 tables as below:
1.Products
product_id, name
1 Books A
2 Books B
3 Books C
4 Books D
5 Books E
2.liked_items
user_id, product_id
1 4
1 3
1 1
I want to query (sql) to retrieve result as below.
Can I do in same single query?
product_id, user_id
1 1
2 0
3 1
4 1
5 0
Hi, this first time i'm posting here.
Hope anyone can help me. Thank you
SELECT Products.product_id,liked_items.product_id,liked_items.user_id
FROM Products
LEFT JOIN liked_items
ON Products.product_id=liked_items.product_id
Try this one,
SELECT a.product_ID,
COALESCE(b.user_id,0) `user_id`
FROM products a
LEFT JOIN liked_items b
ON a.product_ID = b.product_ID
Sometimes liked_items.user_id can be possibly NULL so instead of displaying NULL in the result list, it can be changed using COALESCE. COALESCE is a function that can change NULL value into your desired value.
Click For Demonstration
SELECT
Products.product_id AS product_id,
IFNULL(liked_items.user_id,0) AS user_id
FROM Products
LEFT JOIN liked_items ON liked_items.product_id=Products.product_id
If you're using MS SQL Server you can use IsNull like so:
SELECT p.product_id, IsNull(li.user_id, 0)
FROM products p
LEFT JOIN liked_items li
ON (p.product_id = li.product_id);
If not, Joao or John's answer will do just fine.