Hi this is my tables
table 1 => products
id product price active
3 sample 1500 1
4 sample2 2300 2
table 2 => product_attibute_value
id product option_value
1 3 Green
2 3 Red
and table 3 => product_attribute_options
id product option_value
1 2 8
2 2 7
and my query is
SELECT typ.*
FROM products typ , attibute_value tyav
WHERE typ.active='1' AND ( tyav.option_value = 'Green' ) AND typ.id=tyav.product
GROUP BY typ.id
ORDER BY typ.price ASC
This working fine. Now I want to add product_attribute_options table also.
Now I added new query like this
SELECT typ.*
FROM products typ , attibute_value tyav, product_attibute_options tyop
WHERE typ.active='1' AND ( tyav.option_value = '7' OR tyav.option_value = 'Orange' ) OR
( tyav.option_value = '7' OR tyav.option_value = 'Orange' ) AND
typ.id=tyav.product OR typ.id=tyop.product
GROUP BY typ.id
ORDER BY typ.price ASC
I am showing option value in one page, and if i selected this option like 8 or or orange, it will show the product. this product should come from both attribute_options table and attribute_value table.
Its not coming exactly. What I did mistake in this query? Thanks in advance
Try this:
SELECT p.*
FROM products p
INNER JOIN product_attibute_value pav ON p.id = pav.product
INNER JOIN product_attibute_options pao ON p.id = pao.product
WHERE p.active = 1 AND ( pav.option_value = '7' OR pao.option_value = '7' )
GROUP BY p.id
ORDER BY p.price ASC
It is better if you use JOIN sentences and conditions on each of them.
Try this:
SELECT typ.*
FROM products typ
JOIN attibute_value tyav ON typ.id=tyav.product
JOIN product_attibute_options tyop ON typ.id=tyop.product
WHERE typ.active='1'
AND ( tyav.option_value = '7' OR tyav.option_value = 'Orange' )
GROUP BY typ.id
ORDER BY typ.price asc
Related
As per below MySql Query fetch product count correctly see the red box in image. I need if icat_id 444 than 448 count (158) SUM in category_count (435) of icat_id 444.
After this icat_id 444 total sum of 435 + 158 = 593.
But Case not working properly. Please help
SELECT c.vcategory, c.icat_id,
COUNT( DISTINCT product_cat_rel_1.iprod_id) AS category_count,
UPPER(REGEXP_REPLACE(CONCAT(' ', c.vcategory), ' (.)[^ ]+', '\\1' )) AS category_name,
SUM(CASE WHEN (c.icat_id = '444') THEN 1 WHEN (c.icat_id = '448') THEN 1 ELSE 0 END) AS tt
FROM product
INNER JOIN product_cat_rel_1 ON product_cat_rel_1.iprod_id = product.iprod_id
INNER JOIN product_price ON product_price.iprod_id = product.iprod_id
INNER JOIN category as c ON c.icat_id = product_cat_rel_1.icat_id
WHERE product.estatus='1'
AND product_cat_rel_1.icat_id in (444,448,450,454,471,549,592,741,765,782)
GROUP BY product_cat_rel_1.icat_id
As per requirement group by both 444 and 448 as 444 and sum up corresponding value. Apply changes in given query.
-- MySQL
SELECT c.vcategory,
CASE WHEN product_cat_rel_1.icat_id IN (444, 448)
THEN 444
ELSE product_cat_rel_1.icat_id
END icat_id,
COUNT( DISTINCT product_cat_rel_1.iprod_id) AS category_count,
UPPER(REGEXP_REPLACE(CONCAT(' ', c.vcategory), ' (.)[^ ]+', '\\1' )) AS category_name,
SUM(category_count) AS tt
FROM product
INNER JOIN product_cat_rel_1 ON product_cat_rel_1.iprod_id = product.iprod_id
INNER JOIN product_price ON product_price.iprod_id = product.iprod_id
INNER JOIN category as c ON c.icat_id = product_cat_rel_1.icat_id
WHERE product.estatus='1'
AND product_cat_rel_1.icat_id in (444,448,450,454,471,549,592,741,765,782)
GROUP BY CASE WHEN product_cat_rel_1.icat_id IN (444, 448)
THEN 444
ELSE product_cat_rel_1.icat_id
END
I need to select in one row grouped by a same key other two values in the same field:
TABLE
attribute_id
entity_id
value
DATA
attribute_id|entity_id| value
85| 220| 4740
257| 220|image1.png
And need this result:
attibute_id 85 as SKU, attribute_id 257 as IMAGE in this result:
SKU | IMAGE
4740 | image1.png
How can I do this? TIA!
I think this does what you want:
select ts.entity_id, ts.value as sku, ti.value as image
from t ts join
t ti
on ts.entity_id = ti.entity_id and
ts.attribute_id = 85 and
ti.attribute_id = 257;
You can also solve this using conditional aggregation:
select t.entity_id,
max(case when t.attribute_id = 85 then t.value end) as sku,
max(case when t.attribute_id = 257 then t.value end) as image
from t
group by t.entity_id;
If you have attribute_id|entity_id combinations unique across the table you don't need to group data, just join like this:
http://sqlfiddle.com/#!9/1b2f60/2
SELECT a.entity_id,
a.value AS some_attribute1,
b.value AS image
FROM attribs a
LEFT JOIN attribs b
ON a.entity_id = b.entity_id
AND b.attribute_id = 257
WHERE a.attribute_id = 85
Hi I have a difficulty with one query if someone can help would be great
What I m trying to do is to get all counts by selected category. For example If I haven't selected any category the result is going to be like this:
Category-70 ( 2 ) <- Product-57, Product-56
Category-64 ( 2 ) <- Product-57, Product-50
Category-61 ( 1 ) <- Product-56
Category-73 ( 1 ) <- Product-50
So that's easy. I have a query like this one:
http://sqlfiddle.com/#!9/4f188/1
So I would like to pass category ids to my query, and get counts based on this ids, something like this if, I pass category id 70 the result has to be
Category-70 ( 2 ) <- Product-57, Product-56
Category-64 ( 1 ) <- Product-57, [Product-50 is gone because is not in cateogry id 70]
Category-61 ( 0 )
Category-73 ( 0 )
If I pass category id 70 and 64 the result must be
Category-70 ( 1 ) <- Product-57, [Product-56 is gone because is not in category 70 and 64]
Category-64 ( 1 ) <- Product-57
Category-61 ( 0 ) [Product-56 is gone, because is not in category 70 and 64 ]
Category-73 ( 0 ) [Product-50 is gone because is not in category 70 and 64]
or if I give as parameter category id 73 the result must be
Category-70 ( 0 ) [products are not counted because they are not in 73]
Category-64 ( 1 ) <- Product-50
Category-61 ( 0 ) [products are not counted because they are not in 73]
Category-73 ( 1 ) <- Product-50
Is that even possible :), ty for any help...
+1 for the SQL fiddle example, very useful.
I believe this should fix your problem (added a sub select to your join)
SELECT Concat('Category-',c.category_id),
count(DISTINCT p2c.product_id) as products
FROM category c
LEFT JOIN product_to_category p2c
ON (c.category_id = p2c.category_id AND p2c.product_id) AND p2c.product_id in
(select product_id from product_to_category where category_id = #input)
LEFT JOIN category_path cp ON (cp.category_id = c.category_id AND cp.path_id = c.category_id)
WHERE
cp.level <= 2
GROUP BY c.category_id
ORDER BY c.sort_order
Here is my table structure.(fun_friends)
id user_id,friend_id,status,createdat,updatedat
1 1 2 1 123456 125461
2 1 3 1 454545 448788
3 2 4 1 565659 898889
4 1 5 1 877878 878788
Here is the table structure of user_uploads
id user_id parent_id category_id title slug tags description video_type source video_link video_thumb
1 2 1 2 fun fun ['4','5'] coolvid 1 ytu link thumb
I need to show the latest upload of my friends
Can you tell me how can i join this tables together? i tried with
SELECT * FROM fun_friends WHERE (user_id= '".$_SESSION['user_row_id']."' AND `status` =1) OR (friend_id= '".$_SESSION['user_row_id']."' AND `status` =1)
and it is showing all friends of logged-in user
You can just join both table using user_id field. sample query bellow will return one record with latest user_uploads.id.
select *
from fun_friends a
inner join user_uploads b on a.user_id = b.user id
order by b.id desc limit 0,1
how about using UNION to get the friends user and wrapping it inside a subquery which later join on the other tabel,
SELECT usr.*
FROM user_uploads usr
INNER JOIN
(
SELECT user_ID AS ID
FROM Fun_Friends
WHERE friend_ID = 'ID_HERE'
UNION
SELECT friend_ID As ID
FROM Fun_Friends
WHERE ser_ID = 'ID_HERE'
) idList ON usr.user_ID = idList.ID
I have rows of data from a SELECT query with a few prices (say three for this example). One is our price, one is competitor1 price, one is competitor2 price. I want to add a column that spits out the rank of our price as compared to the other two prices; if our price is the lowest it would spit out the number 1 if the highest it would spit out the number it is out of.
Something like this:
Make | Model | OurPrice | Comp1Price | Comp2Price | Rank | OutOf
MFG1 MODEL1 350 100 500 2 3
MFG1 MODEL2 50 100 100 1 3
MFG2 MODEL1 100 NULL 50 2 2
MFG2 MODEL2 9999 500 NULL 2 2
-Sometimes the competitor price will be NULL as seen above, and I believe this is where my issue lies. I have tried a CASE and it works when only on one competitor but when I add a AND statement it spits out the ranks as all NULL. Is there a better way of doing this through a MySQL query?
SELECT
MT.MAKE as Make,
MT.MODEL as Model,
MT.PRICE as OurPrice,
CT1.PRICE as Comp1Price,
CT2.PRICE as Comp2Price,
CASE
WHEN MT.PRICE < CT1.PRICE AND MT.PRICE < CT2.PRICE
THEN 1 END AS Rank
(CT1.PRICE IS NOT NULL) + (CT2.PRICE IS NOT NULL) + 1 as OutOf
FROM mytable MT
LEFT JOIN competitor1table as CT1 ON CT1.MODEL = MT.MODEL
LEFT JOIN competitor2table as CT2 ON CT2.MODEL = MT.MODEL
ORDER BY CLASS
Not tested, but you can try:
SELECT
a.MAKE AS Make,
a.MODEL AS Model,
a.PRICE AS OurPrice
MAX(CASE WHEN a.compnum = 1 THEN pricelist END) AS Comp1Price,
MAX(CASE WHEN a.compnum = 2 THEN pricelist END) AS Comp2Price,
FIND_IN_SET(a.PRICE, GROUP_CONCAT(a.pricelist ORDER BY a.pricelist)) AS Rank,
COUNT(a.pricelist) AS OutOf
FROM
(
SELECT MAKE, MODEL, PRICE, PRICE AS pricelist, 0 AS compnum
FROM mytable
UNION ALL
SELECT a.MAKE, a.MODEL, a.PRICE, CT1.PRICE, 1
FROM mytable a
LEFT JOIN competitor1table CT1 ON a.MODEL = CT1.MODEL
UNION ALL
SELECT a.MAKE, a.MODEL, a.PRICE, CT2.PRICE, 2
FROM mytable a
LEFT JOIN competitor2table CT2 ON a.MODEL = CT2.MODEL
) a
GROUP BY
a.MAKE, a.MODEL
(CT1.PRICE IS NOT NULL AND CT1.PRICE < MT.PRICE) + (CT2.PRICE IS NOT NULL AND CT2.PRICE < MT.PRICE) + 1 as Rank