MySQL SELECT, JOIN, GROUP BY query optimizing - mysql

I am trying to select DISTINCT products from within categories with category ids (1, 5, 12), ORDERED by cat_order + prod_order from MySQL database
The problem:
if a product is found in more than 1 category I need to show the first result,
ie: product number 1 is assigned to categories 1 and 5, I need to display product number 1 from category 1 along with its prod_order and skip the listing in category 5,
essentually I need to display all products from category 1, than move on to category 5 and display all products from there, where product id was not shown previously, and move on to another category in the list (12)
if I run something like:
SELECT
prod_to_cat.prod_id AS prod_to_cat_prod_id,
prod_to_cat.prod_order AS prod_to_cat_prod_order,
prod_to_cat.cat_id AS prod_to_cat_cat_id,
prod_to_cat.cat_order AS prod_to_cat_cat_order,
products.id,
products.name
FROM
prod_to_cat, products
WHERE
prod_to_cat.prod_id = products.id
AND prod_to_cat.cat_id IN (1, 5, 12)
GROUP BY
prod_to_cat.prod_id
ORDER BY
prod_to_cat_cat_order ASC,
prod_to_cat_prod_order DESC
I get inconsistent results (product 1 will not be selected from the first category in the list), that is why I opted to select without "GROUP BY prod_id" and wrap that with another select which than groups by prod_id.
like so:
SELECT
prod_to_cat_prod_id,
prod_to_cat_prod_order,
prod_to_cat_cat_id,
name
FROM
(
SELECT
prod_to_cat.prod_id AS prod_to_cat_prod_id,
prod_to_cat.prod_order AS prod_to_cat_prod_order,
prod_to_cat.cat_id AS prod_to_cat_cat_id,
prod_to_cat.cat_order AS prod_to_cat_cat_order,
products.id,
products.name
FROM
prod_to_cat, products
WHERE
prod_to_cat.prod_id = products.id
AND prod_to_cat.cat_id IN (1, 5, 12)
ORDER BY
prod_to_cat_cat_order ASC,
prod_to_cat_prod_order DESC
) AS prod
GROUP BY
prod_to_cat_prod_id
ORDER BY
prod_to_cat_cat_order ASC,
prod_to_cat_prod_order DESC
LIMIT 0, 10;
What I am trying to do:
I am trying to find a more efficiant way to do this.
Table structure:
prod_to_cat:
prod_id | cat_id | cat_order | prod_order |
1 1 1 2
2 1 1 0
3 1 1 0
1 5 2 4
4 5 2 0
products:
id | name | descr | price |
1 name_1
2 name_2
3 name_3
4 name_4
each product can be in any number of categories, for example product id 1 is in categories id 1 and 5 in the example above.
Thanks a lot for any replies.
Pasha

You want the groupwise minimum:
SELECT prod_to_cat.*,
products.name
FROM prod_to_cat NATURAL JOIN (
SELECT prod_id,
MIN(cat_id) AS cat_id
FROM prod_to_cat
WHERE cat_id IN (1, 5, 12)
GROUP BY prod_id
) t
JOIN products ON t.prod_id = products.id
ORDER BY prod_to_cat.cat_order ASC,
prod_to_cat.prod_order DESC
See it on sqlfiddle.

Related

Joining multiple columns into one with union, exclude results with same id

I want to join columns from multiple tables to one column, in my case column 'battery_value' and 'technical_value' into column 'value'. I want to fetch data for only given category_ids, but because of UNION, I get data from other tables as well.
I have 4 tables:
Table: car
car_id model_name
1 e6
Table: battery
battery_category_id car_id battery_value
1 1 125 kW
Table: technical_data
technical_category_id car_id technical_value
1 1 5
3 1 2008
Table: categories
category_id category_name category_type
1 engine power battery
1 seats technical
3 release year technical
From searching, people are suggesting that I use union to join these columns. My query now looks like this:
SELECT CARS.car_id
category_id,
CATEGORIES.category_name,
value,
FROM CARS
left join (SELECT BATTERY.battery_category_id AS category_id,
BATTERY.car_id AS car_id,
BATTERY.value AS value
FROM BATTERY
WHERE `BATTERY`.`battery_category_id` IN (1)
UNION
SELECT TECHNICAL_DATA.technical_category_id AS category_id,
TECHNICAL_DATA.car_id AS car_id,
TECHNICAL_DATA.value AS value
FROM TECHNICAL_DATA
WHERE `TECHNICAL_DATA`.`technical_category_id` IN (3))
tt
ON CARS.car_id = tt.car_id
left join CATEGORIES
ON category_id = CATEGORIES.id
So the result I want is this, because I only want to get the data where category_id 1 is in battery table:
car_id category_id category_name technical_value
1 1 engine power 125 kW
1 3 release year 2008
but with the query above I get this, category_id 1 from technical table is included which is not something I want:
car_id category_id category_name value
1 1 engine power 125 kW
1 1 seats 125 kW
1 3 release year 2008
How can get exclude the 'seats' row?
For the results you want, I don't see why the cars table is needed. Then, you seem to need an additional key for the join to categories based on which table it is referring to.
So, I suggest:
SELECT tt.*, c.category_name
FROM ((SELECT b.battery_category_id AS category_id,
b.car_id AS car_id, b.value AS value,
'battery' as which
FROM BATTERY b
WHERE b.battery_category_id IN (1)
) UNION ALL
(SELECT td.technical_category_id AS category_id,
td.car_id AS car_id, td.value AS value,
'technical' as which
FROM TECHNICAL_DATA td
WHERE td.technical_category_id IN (3)
)
) tt LEFT JOIN
CATEGORIES c
ON c.id = tt.category_id AND
c.category_type = tt.which;
That said, you seem to have a problem with your data model, if the join to categories requires "hidden" data such as the type. However, that is outside the scope of the question.

SQL Order results by Match Against Relevance and display the price based on sellers rank

Looking to display results based on 'relevance' of the users search along with the price of the seller that ranks highest. A live example to what i'm after is Amazons search results, now I understand their algorithm is extremely complicated, but i'm after a simplified version.
Lets say we search for 'Jumper' the results that are returned are products related to 'Jumper' but then the price is not always the cheapest is based on the sellers rank. The seller with the highest rank gets his/hers prices displayed.
Heres what I have been working on but not giving me the expected results at mentioned above, and to be honest I don't think this is very efficient.
SELECT a.catalogue_id, a.productTitle, a.prod_rank, b.catalogue_id, b.display_price, b.sellers_rank
FROM
(
SELECT c.catalogue_id,
c.productTitle,
MATCH(c.productTitle) AGAINST ('+jumper*' IN BOOLEAN MODE) AS prod_rank
FROM catalogue AS c
WHERE c.catalogue_id IN (1, 2, 3)
) a
JOIN
(
SELECT inventory.catalogue_id,
inventory.amount AS display_price,
(accounts.comsn + inventory.quantity - inventory.amount) AS sellers_rank
FROM inventory
JOIN accounts ON inventory.account_id = accounts.account_id
WHERE inventory.catalogue_id IN (1, 2, 3)
) AS b
ON a.catalogue_id = b.catalogue_id
ORDER BY a.prod_rank DESC
LIMIT 100;
Sample Tables:
Accounts:
----------------------------
account_id | comsn
----------------------------
1 | 100
2 | 9999
Catalogue:
----------------------------
catalogue_id | productTitle
----------------------------
1 | blue jumper
2 | red jumper
3 | green jumper
Inventory:
-----------------------------------------------
product_id | catalogue_id | account_id | quantity | amount |
-----------------------------------------------
1 | 2 | 1 | 6 | 699
2 | 2 | 2 | 2 | 2999
Expected Results:
Product Title:
red jumper
Amount:
29.99 (because he/she has sellers rank of: 7002)
First, you should limit the results only to the matches for the first subquery:
Second, you should eliminate the second subquery:
SELECT p.catalogue_id, p.productTitle, p.prod_rank,
i.amount as display_price,
(a.comsn + i.quantity - i.amount)
FROM (SELECT c.catalogue_id, c.productTitle,
MATCH(c.productTitle) AGAINST ('+jumper*' IN BOOLEAN MODE) AS prod_rank
FROM catalogue AS c
WHERE c.catalogue_id IN (1, 2, 3)
HAVING prod_rank > 0
) p JOIN
inventory i
ON i.catalogue_id = c.catalogue_id join
accounts a
ON i.account_id = a.account_id
ORDER BY c.prod_rank DESC
LIMIT 100;
I'm not sure if you can get rid of the final ORDER BY. MATCH with JOIN can be a bit tricky in that respect. But only ordering by the matches should help.

How to group duplicated rows and count them?

my table:
id | item_id
1 | 5
2 | 5
3 | 7
4 | 2
sql:
$countWeek = $conn->query("SELECT count(item_id) FROM `myTable` GROUP BY `item_id`")->fetchColumn();
As you can see i have 2 duplicated rows with item_id = 5 i want to group these duplicated rows and output 3 rows on the count, but when i do echo $countWeek it output 1, why?
When i change the above sql to:
$countWeek = $conn->query("SELECT item_id FROM `myTable` GROUP BY `item_id`")->rowCount();
It returns the correct value, but i don't want to use rowCount() because i only need to count the rows and fetchColumn() with count() is far better in terms of speed.
You could use counct(distinct item_id)
SELECT count(distinct item_id)
FROM `myTable`

MYSQL: Select entries in relation to multiple rows of multiple table

I have three tables that I can't change the structure of:
facet
id name
-----------------
1 Series
2 Material
value
id facet_id name
----------------------------------
1 2 Glass
2 2 Metal
3 1 Series #1
4 1 Series #2
5 1 Series #3
product_facet_values
product_id value_id
-----------------------------------
1 1
1 3
2 1
2 4
3 2
3 5
4 1
I am trying to write two queries:
/1. One that will return the ids that represent the series facet from the values table where a product record is Glass and has any series. So an output like this:
id facet_id name
----------------------------------
3 1 Series #1
4 1 Series #2
Record 1 is not a series.
Record 2 is not a series.
Record 3 is returned because product #1 has both a series and material and the material is glass.
Record 4 is returned because product #2 has both a series and material and the material is glass.
Record 5 is not returned because product #3 has the material of metal even though it has both a series and material.
/2. Same as number one but return a list of product ids.
product_id
---------------
1
2
Product #1 is returned because it has both a series and material and the material is glass.
Product #2 is returned because it has both a series and material and the material is glass.
Product #3 is not returned because it has the material of metal even though it has both a series and material set.
Product #4 is not returned because it has no series set even though the material is glass.
FIRST QUERY:
try this.. I believe this will do the trick.
SELECT
v.id
FROM value v
JOIN product_facet_values pfv ON pfv.value_id = v.id
WHERE pfv.product_id IN
( SELECT
product_id
FROM product_facet_values
WHERE
product_id IN
( SELECT
product_id
FROM product_facet_values
GROUP BY product_id
HAVING COUNT(*) > 1
)
AND value_id = 1
)
AND v.facet_id = 1;
SECOND QUERY:
the inner part of the same query returns the products that have a value of 1 and a value of something other than one so it would be this
SELECT
product_id
FROM product_facet_values
WHERE
product_id IN
( SELECT
product_id
FROM product_facet_values
GROUP BY product_id
HAVING COUNT(*) > 1
)
AND value_id = 1
EXPLANATION:
INNERMOST SUBQUERY:
SELECT
product_id
FROM product_facet_values
GROUP BY product_id
HAVING COUNT(*) > 1
give me products that have more than one record per product (later to be filtered by 'Glass' and 'Series')
MIDDLE SUBQUERY:
SELECT
product_id
FROM product_facet_values
WHERE
product_id IN
(
INNERMOST SUBQUERY
)
AND value_id = 1
give me products that have the value_id = 1 (aka 'Glass') that have more than one record.
OUTERMOST QUERY:
SELECT
v.id
FROM value v
JOIN product_facet_values pfv ON pfv.value_id = v.id
WHERE pfv.product_id IN
(
MIDDLE SUBQUERY
)
AND v.facet_id = 1;
give me the value id for the products that are related to 'Glass' but whos facet_id = 1 (aka series)

mysql search keywords in string table

I have a catID column in product table, it is contain category ids as string,
Something like that '142,156,146,143'
and i Have a query '?catID=156,141,120'
i want to search each id in catID column.
I use this query:
SELECT * FROM product WHERE catID REGEXP '156|141|120'
this code return products which have any id in catID column , but I want to return products which is have all id,
So , I'am looking for and operator in REGEXP , but I'am couldn't find.
I want to use REGEXP or something like that which function provide to find product with one query , I don't wan to use
catID LIKE '156' AND catID LIKE '141' ....
if it is posibble.
EDIT : I don't want to perform a function one more time , because the query can be have 100+ id so it's make more harder to write code,
You need to use find_in_set() for each category id parameter in order to find the values in set,also if you can alter the schema then do normalize it, by having another junction table which holds the relation from this table to category table
select * from
product
where
find_in_set('142',catID ) > 0
For multiple values like find_in_set('161,168,234,678',preferred_location ) > 0 no it can't be possible doing like this you have to perform for each location id like
select * from
product
where
find_in_set('142',catID ) > 0
and find_in_set('156',catID ) > 0
and find_in_set('146',catID ) > 0
and find_in_set('143',catID ) > 0 ... for more
Database normalization
find_in_set
Sample Schema
Table
Products (id,other columns)
Categories (id,other columns)
Product_categories (id,product_id,category_id)
Product_categories is a junction table which will hold product_id and one category_id per each product so each will have a relation with single category and single product at a time
For example
Products
id name
1 product 1
2 product 2
Categories
id name
142 category 1
156 category 2
146 category 3
143 category 4
Product_categories
id product_id category_id
1 1 142
2 1 156
3 1 146
4 1 143
Now you can join these tables and query like below using in() and count should be equal to the no of category ids provided as parameter
select p.* from
Products p
join Product_categories pc on (p.id = pc.product_id)
where pc.category_id in(142,156,146,143)
group by p.id
having count(distinct pc.category_id) = 4
Sample Demo
or if you can't count the provided category ids as parameter you can do this by following query
select p.* from
Products p
join Product_categories pc on (p.id = pc.product_id)
where pc.category_id in(142,156,146,143)
group by p.id
having count(distinct pc.category_id) =
ROUND (
(
LENGTH('142,156,146,143')
- LENGTH( REPLACE ( '142,156,146,143', ",", "") )
) / LENGTH(",")
) + 1
Sample Demo 2