MySQL JOIN from 2 tables with same ID using PHP - php

I got a table like:
product
id | product_id
1 55
2 56
3 57
product_options
id | product_id | options_value
1 55 88
2 55 87
3 55 89
... ...
I want to select all option_values from product_options where product_id from product is same with product_id from product_options.
After I select all fields from product I use this:
$sql .= " LEFT JOIN " . DB_PREFIX . "product_option_value ovi ON (ovi.product_id=p.product_id)";
if(...){
$sql .= " AND ovi.option_value_id='$value'";
}
The problem is: If I only got one options_value, it's fine.
but when I have 2 or more options_values the result is 0.
i want to select all options_value from product_options for all product_id from product_options
PS. Sorry for my english and explication

Use right join with product_id
select p.id, p.product_id, po.options_value from products p right join product_options po on p.product_id=po.product_id

Use inner join between two table using product_id as join key
select p.id, p.product_id, po.optional_value
from products p inner join product_options po on p.product_id=po.product_id

Related

Fetch brands id name from brand table if that brand id is present in products table

I've two tables: companies and products
Companies
id
name
36
BMW
37
MERCEDES
39
LG
40
MI
42
APPLE
44
ONE PLUS
45
GODREJ INTERIO
46
USHA
47
NILKAMAL
Products
id
brand
cat_id
1
44
11
5
39
11
6
40
11
7
44
11
10
0
0
11
0
27
12
0
21
13
42
11
14
0
0
I need to fetch brand name and brand id from companies table if that brand id available in products table where category id is equal to 11
Output should be like below
id
name
39
LG
40
MI
42
APPLE
44
ONE PLUS
SELECT b.id, b.name, b.media_file
FROM wo_products p
LEFT JOIN wo_companies b
ON p.brand = b.id
WHERE category = 11 AND user_id = 1
ORDER BY p.id DESC
enter image description here
I'm getting well result from above query but brand id is repeated multiple times.
If I understand correctly, you want an exists query, which is phrased very similarly to your question:
select c.*
from companies c
where exists (select 1
from products p
where p.brand = c.id and p.cat_id = 11
);
That's the Solution
SELECT b.id, b.name, b.media_file
FROM wo_products p
LEFT JOIN wo_companies b
ON p.brand = b.id
WHERE category = 11 AND user_id = 1
GROUP BY id ORDER BY `name` ASC;
Final Solution
SELECT DISTINCT b.id, b.name, b.media_file
FROM wo_products p
LEFT JOIN wo_companies b
ON p.brand = b.id
WHERE p.category = 11 AND p.user_id = 1 AND p.brand != ''
GROUP BY id ORDER BY `name` ASC;
If you don't mind a slight performance loss you can go with subqueries which are a bit easier to reason about:
SELECT b.id, b.name, b.media_file
FROM wo_companies b
WHERE b.id in (
SELECT p.brand
FROM wo_products p
WHERE p.cat_id = 11 AND p.user_id = 1
)

Cannot get data from two tables using join

First table: Product
id | Name | Price | Discount
-------------------------------
1 xyz 200
2 xyz 250
3 yz 100 50
Second table : buy
id | userid | Product_id Card_details
------------------------------------------
1 1 1 55555
2 1 2 88888
3 3 1 77777
Now i have $user_id in my php variable. If user id is 3 i want following output:
id Name Price Discount user_id Product_id Card_details
3 yz 100 50 3 1 77777
How can i achieve this.
Use JOIN or where
SELECT * FROM Product p JOIN buy b
ON p.id = b.Product_id
WHERE b.userid ='3'
SELECT * FROM Product JOIN buy ON Product.id = buy.id WHERE buy.userid = $user_id;
Try the following query
$query = "SELECT Product.id, Product.Name, Product.Price, Product.Discount,
buy.userid , buy.Product_id, buy.Card_details
FROM Product
JOIN buy ON Product.id = buy.Product_id
WHERE buy.userid = 3";
First in your Product table xyz is to time you have to use unique name for your product for better understanding.
Now in Product table id is your product id. Check your Buys tables productid there is no entry for product id 3. When you want to buy product 3 for user id 3 then buy table productid entry should be three for userid 3.
Now check the below query it will definitely work for you.
SELECT * FROM Product
INNER JOIN buy ON Product.id = buy.Product_id
WHERE buy.userid = '3';
It means you want to get product buy by userid 3.
Try This
DECLARE #USER_ID INT=3 --I/P of USER_ID
SELECT P.ID,
P.NAME,
P.PRICE,
P.Discount,
B.user_id,
B.PRODUCT_ID,
B.CARD_DETAILS
FROM #PRODUCT P
JOIN #BUY B
ON P.ID = B.ID
WHERE B.USER_ID = #USER_ID

Mysql Join with WHERE min(value)

I have this problem
I have 2 Tables
1. product_detail
-id
-name
-thumb
2. product_sale
-id
-pid
-fid
-price
-package
.
SELECT *
FROM product_sale
WHERE MIN(product_sale.price)
JOIN product_detail
ON product_detail.id = product_sale.pid
ORIGINAL SELECT SORRY POSTED WRONG
SELECT * FROM product_detail INNER JOIN product_sale ON product_detail.id = pid
I Have 1 Product in product_detail Example Product1
I Have Many rows in product_sale for Product1 but different product_sale.fid and product_sale.price
What i need to do ist make a select that display Product1 With Lovest Price in product_sale
1 Product1 1.50 1pz
2 Product2 2.50 3pz
3 Product3 3.00 1pz
Now i Get
1 Product1 1.50 1pz
2 Product1 1.65 1pz
3 Product1 1.70 1pz
4 Product3 3.00 1pz
Please helpme what select i need to do ??Thank You All
Barring subqueries, you can't JOIN after a WHERE, and I am pretty sure you can't use MIN in a WHERE either. However, you can WHERE a subquery that contains a MIN.
SELECT *
FROM product_sale AS ps INNER JOIN product_detail AS pd ON ps.pid = pd.id
WHERE (ps.pid, ps.price) IN (
SELECT pid, MIN(price)
FROM product_sale
GROUP BY pid
);
Note that if the product is sold at the lowest price multiple times, you will get multiple rows for it. I am not clear on what the #pz values are supposed to be, but perhaps the * in my answer should be pd.id, pd.name, COUNT(1) AS pz and GROUP BY pd.id, pd.name placed before the ;
Edit: The below should get the "last sale" asked about in the comments below this answer.
SELECT *
FROM product_sale AS ps3
INNER JOIN product_detail AS pd ON ps3.pid = pd.id
WHERE ps3.fid IN (
SELECT MAX(ps2.fid) AS lastFidsForPidsAtLowestPrices
FROM product_sale AS ps2
WHERE (ps2.pid, ps2.price) IN (
SELECT ps1.pid, ps1.MIN(price) AS lowestPriceForPid
FROM product_sale AS ps1
GROUP BY ps1.pid
)
GROUP BY ps2.pid
)
;
Seems to be a straight forward join with a group by and a min..
SELECT PD.name, min(price) as LowestSoldPrice, PD.Thumb, PS.Package
FROM product_Detail PD
LEFT JOIN product_Sale PS
on PD.ID = PS.PID
GROUP BY PD.name, PD.Thumb, PS.Package
I don't understand your query at all. I was surprised it produced anything but an error. This is what I would do.
select pd.id productid,
min(pd.name) name, -- min just so I do not have a complex group by
min(ps.price) minprice
from product_detail pd
join product_sale ps
on pd.id=ps.pid
group by pd.id
This will give you one row per product ID with the name of the product and the min price.
SELECT ps.*, pd.*
FROM product_sale ps
LEFT JOIN product_sale t
ON ps.pid = t.pid AND ps.price>t.price
LEFT JOIN product_detail pd
ON pd.id = product_sale.pid
WHERE t.pid IS NULL
But as many people here I wonder if you really need GROUP BY. What is accepted behavior if you have 2 or more sales with the same minimum price? Should all records be returned? or just one? which one then?

Mysql : Products which satisfies category conditions

I want only those products which satisfies following condition :
Which have ((category_id = 1 OR category_id = 2) AND (category_id = 3 OR category_id = 4))
Database structure:
products
id int(10)
name varchar(128)
categories
Id int (10)
name varchar(64)
category_products
product_id int(10)
category_id int(10)
Data:
My product details are:
Products:
id name
1 P1
2 P2
3 P3
4 P4
My category details are:
Categories:
Id name
1 C1
2 C2
3 C3
4 C4
My category-product mapping details are:
category_products:
product_id category_id
1 1
1 3
1 4
2 2
3 1
3 2
3 3
3 4
4 1
4 4
Final Output should be:
id
1
3
4
SELECT DISTINCT p.id, p.name FROM products AS p
INNER JOIN category_products AS c1 ON c1.product_id=p.id
INNER JOIN category_products AS c2 ON c2.product_id=p.id
WHERE c1.category_id IN (1,2) AND c2.category_id IN (3,4)
It wouldn't work to use
WHERE c.category_id IN (1,2) AND c.category_id IN (3,4)
because a WHERE clause tests just one row at a time, and there's no way one category_id can have two values on a single row. So the self-join handles that, by matching more than one row and referencing them by correlation names.
Try this:-
select *
from products
where id in (select distinct(product_id)
from category_products
where (category_id =3 0R category_id=4)
AND (category_id =1 OR category_id=2))
SELECT p.product_id AS id FROM product_categories AS p
WHERE (p.category_id=1 OR p.category_id=2) AND (p.category_id=3 OR p.category_id=4) GROUP BY p.category_id
Because you don't need the name of the product you don't need an INNER JOIN. GROUP BY and DISTINCT should both work.

join two queries to form an result set

hello all i have to join two queries to form an result set :-
1st:
SELECT
orderId,
GROUP_CONCAT(DISTINCT(categoryId) ORDER BY orderId SEPARATOR ', ') as catId
FROM
ecart_product
INNER JOIN ecart_orderdetail
WHERE
ecart_orderdetail.productId = ecart_product.id
group by orderId
this gives me
orderId catId
167 59, 2
168 2
169 2
170 2
171 2
172 48, 2
173 2
174 2
2nd:
select * from ecart_orders
in both orderId is common how could this could be join ?
Assuming that orderId is PK in ecart_orders table and that it has productId column.
You can try this:
SELECT
orderId,
GROUP_CONCAT(DISTINCT(categoryId) ORDER BY orderId SEPARATOR ', ') as catId ,
ecart_orders.*
FROM
ecart_product
INNER JOIN ecart_orderdetail
ON ecart_orderdetail.productId = ecart_product.id
INNER JOIN ecart_orders
ON ecart_orders.productId = ecart_product.id
group by orderId
In mean time i also solved it
select * from ecart_orders Inner join(
SELECT
orderId,
GROUP_CONCAT(DISTINCT(categoryId) ORDER BY orderId SEPARATOR ', ') as catId
FROM `ecart_product` INNER JOIN ecart_orderdetail
WHERE ecart_orderdetail.productId = ecart_product.id group by orderId) as c
on ecart_orders.id=c.orderId

Categories