Select non registred values in MYSQL - php

I have two tables in Mysql, Products and Inventory...
Products
int id_product
varchar name
Inventory
id_inventory
product_id
existence
So when my product '3' is out of stock I delete the row in Inventory where product_id = 3
And now I need to get all values that I have deleted on Inventory cause I need to make a report of missing products...
How can I make this query?
I used SELECT i.* FROM inventory i, products p WHERE i.product_id!=p.id_product but it doesn't works..
Thank you

LEFT JOIN would do as you wish as the rows you are looking for would have no corresponding row in inventory for a row in products, and so that gives the query ...
SELECT
p.product_id
FROM product p
LEFT JOIN inventory i
ON i.product_id = p.product_id
WHERE i.product_id IS NULL
Alternatively you could simply just not delete the row from inventory when unavailable. Just then do something like
SELECT
p.product_id
FROM product p
INNER JOIN inventory i
ON i.product_id = p.product_id
AND i.quantityInStock = 0
Either way works

or
select p.* from product p
where not exists (select null from inventory i where i.product_id=p.id_product)

Related

MYSQL Query Calculation in relationship

MySQL Schema
product_categories:
id, name
products
id,category_id,name,description
product_offers
id,product_id,name,email
product_offer_negotiations
id,product_offer_id,offer(float),by_user(ENUM-Seller/buyer),status
This is there query i have tried...
select products.*,
product_categories.name as category_name,
COUNT(product_offers.id) AS total_offers,
ROUND( AVG(product_offer_negotiations.offer), 2) AS avg_offer
from products
inner join product_categories on product_categories.id = products.category_id
inner join product_offers on product_offers.product_id = products.id
inner join product_offer_negotiations on product_offer_negotiations.product_offer_id = product_offers.id
and product_offer_negotiations.by_user = ?
group by products.id;
I would like to get all fields from product table, total_offers and avg_offer offer for product (only highest value from negotiation table per offer_id - no duplicatation)
This query only returns first row of products table. Thanks for help.

mysql query to fetch all products with no orders for a user

I have a Products table and an Orders table defined in such a way that I can do JOIN query as the following to return Products with zero orders for a specific user.
This query works but its very slow.
select * from products where id not in (select product_id from orders where user_id = 1)
The question is, how to write same query better way and faster?
No need of a subquery for that:
SELECT p.product_id
FROM
Products p
LEFT JOIN Order o ON p.product_id = o.product_id AND o.user_id = #UserId
WHERE
o.order_id IS NULL -- or any other field that cannot be null on Order
EDIT: for increased performance you may want to check as well that you have indexes in place on the Order user_id column and on your ids (more likely you have them there and probably clustered indexes, both worth to check)
You should be able to do simple LEFT JOIN
SELET * FROM products
LEFT JOIN orders ON (orders.product_id=products.id and orders.user_id=1)
WHERE orders.id IS NULL;
SELECT P.*
FROM products P
LEFT JOIN (SELECT product_id from orders where user_id = 1) O
ON P.id = O.product_id
WHERE O.product_id IS NULL

How to order the category that has most product

I'm not so clear in one of mysql.
i have two tables:
tblcategory
id, name
tblproduct
id, Name, Qty, Price, Category_id
and when i use sql to select all catogories to order by product count:
SELECT c.id,c.name,count(p.id) as product_count
FROM tblcategory as c inner join tblproduct as p on c.id=p.category_id
GROUP BY c.id,c.name
ORDER BY product_count;
The result was that some category which has no product was not appear in my result! how can i get all of them?
You need to use a left outer join:
SELECT c.id,c.name,count(p.id) as product_count
FROM tblcategory as c left outer join tblproduct as p on c.id=p.category_id
GROUP BY c.id,c.name
ORDER BY product_count;
The inner join only keeps records that match in both tables. You want all the product categories, even when there are no matches. The left outer join keeps all records in the first table (tblcategory) along with any matching records in the second table (tblproduct). If there are no products on a category, then you'll get a value of 0 instead of a missing row.

MySQL inner join with two tables

I'm very new to MySQL and PHP and I'm struggling with inner joins between two tables. I'm constructing a script that reads an os commerce database and tells me which products is currently on back order. In order for the product to be on back order a value in the products_attributes table is set to '134', however it only reads the product_id and not the product_model which is in the 'products' table.
products_attributes(table name)
options_values_id
product_id
products(table name)
product_id
product_model
I want to select items that have the value of '134' in the products_attributes table then match the product_ids from both tables to get the product_model from the "products" table. I feel like the code is very easy but for some reason I'm struggling with how to construct the query and to display it.
SELECT product_model FROM products as p,products_attributes as pa WHERE p.product_id = pa.product_id and pa.options_values_id = 134
or
SELECT p.product_model FROM products p INNER JOIN products_attributes as pa ON (p.product_id = pa.product_id) WHERE pa.options_values_id = 134
I think that you're asking how to get the product ID of anything have an attribute of 134
In that case, no JOIN is required.
SELECT DISTINCT
product_id
FROM
products_attributes
WHERE
options_values_id = 134
If you want the product_model then you could form the JOIN like this
SELECT DISTINCT
p.[product_model]
FROM
products p
JOIN
product_attributes pa
ON pa.[product_id] = p.[product_id]
WHERE
pa.[options_values_id] = 134
So the JOIN stipulates how the two tables are related to each other. Once there is a valid JOIN you can use the joined table nearly anywhere in the query.
You can try the following:
select p.product_id, p.product_model
from products p
inner join products_attributes pa on pa.product_id = p.product_id
where pa.options_values_id = '134'

Fetching product categories in one row with product info

I have 3 tables
products categories products_categories
------------ ------------- --------------------
product_id category_id product_id
product_title category_title category_id
Each product can belong to multiple categories as the db schema shows.
I join the three tables to get a listing of the three last inserted products.
SELECT p.product_id,
p.product_title,
c.category_id,
c.category_title
FROM products p
INNER JOIN products_categories pc ON p.product_id = pc.product_id
INNER JOIN categories c ON pc.category_id = c.category_id GROUP BY p.product_id LIMIT 3
With the above query I get the products but with the first category that the product belongs to.
My question is, is it possible by modifying the query to get all the categories that each product belongs to, in one row along with the product info?
Or the only way is to execute another query for each product while fetching the list to get its categories?
You could try:
SELECT p.product_id, p.product_title,
GROUP_CONCAT(c.category_title)
FROM products p
INNER JOIN products_categories pc ON p.product_id = pc.product_id
INNER JOIN categories c ON pc.category_id = c.category_id
GROUP BY p.product_id
See GROUP_CONCAT syntax here
Dont use "Group By". Then in your PHP Code group manually to product_id and collect all categories.

Categories