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
Related
I came across two tables something like this
product table -
id
product
1
product1
2
product2
product_inventory table -
product_id
quantity
1
5
2
1
3
9
the product_inventory tables product_id supposed to reference product tables id but let's say it doesn't so we have a product_id (and quantity) in Product inventory table that's not associated with any real product. Now imagine there are hundred thousands of invalid values like this so I can't manually remove them by id. So I find these using right joining both tables like this:
SELECT product.id, product_inventory.product_id
FROM product
RIGHT JOIN product_inventory ON product.id=product_inventory.product_id
where product.id is NULL;
so I get list of like this for example
product.id
product_inventory.product_id
1
5
2
1
NULL
9
so how do I delete from product_inventory table where product.id is NULL?
You can use:
delete pi from product_inventory pi
where not exists (select 1 from product p where p.id = pi.product_id);
You should also learn about foreign key relationships to prevent this from happening.
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
I have two tables, the first is the products table, and it has 2 columns: id and product_name.
The second table is the filters table, and it has 2 columns: filter_id, product_id.
For example i have this in the products table:
id | product_name
1 | test product
and this in the filters table:
filter_id | product_id
1 | 1
2 | 1
3 | 1
As you can see, the product with id '1' has 3 filters.
My goal is to get the products by filters.
For example, i need every product where the product has the 2 and 3 filter_id. I tried to use something like that:
SELECT * FROM products p LEFT JOIN filters f ON (p.id = f.product_id) WHERE (filter_id = '2' AND filter_id = '3')
I can't do that because the filter_id can't be equal to 2 and 3 at the same time.
The main problem is that i should use only one query to get the products.
Group by the product and select only those having both filter_ids
SELECT p.id, p.product_name
FROM products p
JOIN filters f ON p.id = f.product_id
WHERE f.filter_id in (2,3)
group by p.id, p.product_name
having count(distinct f.filter_id) = 2
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 a table for products and a table for users who have bought products. On the products table A there is site_name which determines where the products were bought.
Table B shows the users and what they have bought.
I am using the following to show a list of products bought, by site_name, grouping the product name together.
SELECT product FROM A JOIN B ON A.prod_id = B.prod_id WHERE A.site_name = 'ebay' group by A.product
Table A for products is:
prod_id
site_name
product
Table B for users is:
user_id
prod_id
What i can't figure out is how to get the number of products bought per line.
e.g. in table A there is
prod_id site_name product
------- --------- -------
1 ebay chair
2 amazon desk
3 ebay lamp
and on table b
user_id prod_id
------- -------
1000 1
1001 2
1002 1
1003 3
So I want to show each line where site_name is ebay and how many products were bought, order by most first like:
chair 2
lamp 1
I would use a LEFT JOIN, so that if there is a product that has never been purchased, it will show up with a count of 0.
Also, I would use COUNT(users.prod_id), instead of COUNT(*), so that it will only count rows which have satisfied the LEFT JOIN condition:
SELECT
products.product,
COUNT(users.prod_id) AS productsBought
FROM
A AS products
LEFT JOIN B AS users
ON products.prod_id = users.prod_id
WHERE products.site_name = 'ebay'
GROUP BY products.product
A minor change to Michael's query.
SELECT
products.product,
COUNT(users.prod_id) AS productsBought
FROM
A AS products
INNER JOIN B AS users
ON products.prod_id = users.prod_id
WHERE products.site_name = 'ebay'
GROUP BY products.product
The above query will not return products that haven't been bought.