Print number of orders each products_id - php

Is there a faster solution than mine?
I have a table with columns orders_id and products_id (can't change that)
Now I like to know how many orders made with unique products_id.
I tried the following. But it crashes while loading tooo many rows (160'000).
SELECT DISTINCT `products_id` , `products_name` , COUNT(*) as totalorders FROM `orders_products` ORDER BY `products_id` ASC
I tried also with subqueries, but same problem as above.
SELECT (SELECT count(*) FROM orders_products WHERE products_id = op.products_id) AS totalorders, products_id, products_name FROM orders_products op ORDER BY products_id ASC

Hopefully you have an index on products_id
In that case, I would try something like the following:
select products_id, Min(Products_Name), count(*) as TotalOrders
from Orders_Products
Group by Products_ID
Order by Products_ID asc
There are a couple of other ways that you could grab the products_name, such as including it in your grouping (some other implications there).

How about:
SELECT `products_id`, `products_name`, COUNT(*) FROM `orders_products` GROUP BY (`products_id`, `products_name`) ORDER BY `products_id`

Related

Searching multiple databases to output lowest value

$query = mysqli_query($mysqli, "SELECT product_name ,MIN(product_price) product_price,link FROM(
select jumia.product_name,jumia.product_price,jumia.product_image,jumia.link from jumia INNER JOIN
jumia ON jumia.id = olx.id
where product.name like '%{$search}%'
UNION
select olx.product_name,olx.product_price,olx.product_image, olx.link from olx INNER JOIN
olx ON olx.id = jumia.id
where product.name like '%{$search}%')Minim
GROUP BY product_name,product_image
");
I am trying to create a query from two tables with similar column names as displayed above that will allow me to display the rows between the two tables that have the lowest price.
For example, if product_name called mattresses is searched the matching item in my database whose price is lower between the two table names should be displayed. Some help would be appreciated
I think this is the general idea of what you're trying to do:
SELECT id, price
FROM (SELECT id, price FROM T1 WHERE price = (SELECT min(price) FROM T1)
UNION
SELECT id, price FROM T2 WHERE price = (SELECT min(price) FROM T2)
) AS M
ORDER BY price ASC
LIMIT 1
Ended up changing the code and removing the ordering at the end and this finally worked. I hadn't properly linked my database using foreign keys, and changed my code to reflect this.
$query = mysqli_query($mysqli, "
SELECT product_name
, MIN(product_price) product_price
, link
FROM
( select j.product_name
, j.product_price
, j.link
from jumia j
JOIN olx
ON j.categoryID = olx.categoryID
where j.product_name like '%{$search}%'
UNION select olx.product_name
, olx.product_price
, olx.link
from olx
JOIN jumia
ON jumia.categoryID = olx.categoryID
where olx.product_name like '%{$search}%'
) x
");

Why Mysql order by doesn't work for this query

I have this query:
SELECT *
FROM (
SELECT p.id, product, unique_name, price, old_price, category_id, added_date
FROM products p, products_to_categories ptc, products_to_adverts pta
WHERE p.id=ptc.product_id AND (expire_date > now() OR expire_date=0)
AND p.id=pta.product_id AND p.active=1 AND p.instock=1 AND p.top_product="1"
and p.id not in (58,59,70,88,92,106,107,108,109)
and pta.advert_id not in (1,4,5,6,7,9,13,15,17)
ORDER BY added_date DESC
) as t GROUP BY id LIMIT 0,32
added_date field is datetime
Thanks !
You cannot use order by in subquery. Try using temporary table instead.
You can not use order by like this inside the query .As I think this is wrong.If you can do this using sql CASE
Follow this article.
[http://www.mysqltutorial.org/mysql-case-statement/][1]

How to count items in a table based on the main query?

Okay guys say I am currently doing this query:
SELECT `category_id`, `category_name`
FROM `database_categorys`
ORDER BY `category_name` ASC
Now I want to count all rows from a table called "database_items" where the item table's category id is equal to the current rows category id.
Some sort of join or nested query I am guessing but I cannot wrap my head around the correct syntax to do it.
So that when echoing it out I can do:
<category name> <total items in category number>
SELECT c.`category_id`, c.`category_name`, count(i.`category_id`)
FROM `database_categorys` c
LEFT OUTER JOIN `database_items` i on c.`category_id` = i.`category_id`
GROUP BY c.`category_id`, c.`category_name`
ORDER BY c.`category_name`
I would use subqueries like this:
SELECT
`category_id`, `category_name`,
(SELECT count(1) from database_items i where i.category_id = c.category_id)
FROM `database_categorys` c
ORDER BY `category_name` ASC

MYSQL return rows with number of column relationships in another table

I have a table categories and table posts . I want to return categories that have more than 3 posts.
My query
SELECT `categories`.`category_title`, COUNT(posts.post_id) as total_posts
FROM (`categories`)
JOIN `posts` ON `posts`.`category_id` = `categories`.`category_id`
HAVING `total_posts` > 3
ORDER BY `categories`.`date_created` desc
it returns just 1 row.. What is the correct way to do this type of query without using 2 queries?
Your query is making use of a MySQL feature called "hidden columns" and you might not even know it. This is because your query is referencing elements, such as date_created, which should be aggregated but are not ("should" here means according to the SQL standard and most other databases).
The problem with your query is that it is missing the group by. An alternative way of writing this is with the aggregation in a subquery, before joining to category:
SELECT `categories`.`category_title`, total_posts
FROM `categories` JOIN
(select categoryid, COUNT(posts.post_id) as total_posts
from `posts`
group by categoryid
having count(*) > 3
) pc
ON `pc`.`category_id` = `categories`.`category_id`
ORDER BY `categories`.`date_created` desc
You need to group the items by category.
SELECT `categories`.`category_title`, COUNT(posts.post_id) as total_posts
FROM (`categories`)
JOIN `posts` ON `posts`.`category_id` = `categories`.`category_id`
GROUP BY `categories`.`category_id`
HAVING `total_posts` > 3
ORDER BY `categories`.`date_created` desc

MySQL Query with DISTINCT and ORDER BY

Here is my query:
$result = $mysqli->query('SELECT DISTINCT SKU_SIZE_PART1
FROM SKU_DATA
ORDER BY SKU_SIZE_PART1 DESC');
Now this works perfect for SKU_SIZE_PART1 but I have 2 more parts that I need to grab. Now when I put a comma and do this: 'SKU_SIZE_PART1, SKU_SIZE_PART2, SKU_SIZE_PART3' then the DISTINCT doesn't work and I get a ton of duplicates, and then I'm not sure how to order the query so that all of them are ordered by the size and DESC.
Does that make sense? I could just duplicate that query 2 more times and have 3 separate queries but I would like to know how to accomplish this with just one.
I'm not positive that I understand what you're trying to do, but it sounds like you might actually want something like this:
SELECT SKU_SIZE_PART1 AS SKU_SIZE_PART
FROM SKU_DATA
UNION
SELECT SKU_SIZE_PART2 AS SKU_SIZE_PART
FROM SKU_DATA
UNION
SELECT SKU_SIZE_PART3 AS SKU_SIZE_PART
FROM SKU_DATA
ORDER BY SKU_SIZE_PART DESC
which will return all distinct SKU_SIZE_PART1/2/3 values in a single column, rather than all distinct (SKU_SIZE_PART1, SKU_SIZE_PART2, SKU_SIZE_PART3) triads in three columns.
After reading your question several times, I figured this might be what you are looking for:
SELECT SKU_SIZE_PART1 AS ssp
FROM SKU_DATA
UNION
SELECT SKU_SIZE_PART2 AS ssp
FROM SKU_DATA
UNION
SELECT SKU_SIZE_PART3 AS ssp
FROM SKU_DATA
ORDER BY ssp DESC
SELECT d.sku_size_part1, d.sku_size_part2, d.sku_size_part3
FROM sku_data d
WHERE d.id IN (
SELECT s.id <<--- replace `id` with the real primary-key for table `sku_data`
FROM sku_data s
GROUP BY s.sku_size_part1)
ORDER BY d.sku_size_part1 DESC
Note that this will select rows more or less at random.
Although all sku_size_parts will be from the same row, lots of values will be hidden.
If you want to make the query stable, you need to add a having clause in the inner subselect.
Something like this:
SELECT d.sku_size_part1, d.sku_size_part2, d.sku_size_part3
FROM sku_data d
WHERE d.id IN (
SELECT s.id <<--- replace `id` with the real primary-key for table `sku_data`
FROM sku_data s
GROUP BY s.sku_size_part1
HAVING s.sku_size_part2 = MIN(s.sku_size_part2)
AND s.sku_size_part3 = MIN(s.sku_size_part3))
ORDER BY d.sku_size_part1 DESC
Either that or you want #bfavaretto's UNION variant.
DISTINCT selects a distinct set of rows, not columns... the assumption/problem here is how to condense multiple columns. If you had the following table
sku1 | sku2 | sku3
---------------------
a | a | b
b | b | b
Telling it to select destinct would return both rows because none of them are distinct, you couldn't just remove the third column because then the row data would be inconsistent. If you want everything in one table you can do this with subqueries.
SELECT (SELECT DISTINCT SKU_SIZE_PART1 FROM SKU_DATA ORDER BY SKU_SIZE_PART1 DESC)
as part1, (SELECT DISTINCT SKU_SIZE_PART2 FROM SKU_DATA ORDER BY SKU_SIZE_PART2 DESC)
as part2, (SELECT DISTINCT SKU_SIZE_PART3 FROM SKU_DATA ORDER BY SKU_SIZE_PART1 DESC)
as part3 FROM SKU_DATA
You can read up a little on how DISTINCT works to see why you can't just do SELECT DISTINCT SKU_SIZE_PART1, PART2, PART3. Somewhere like This Link

Categories