Count the same order_id and output how many in SQL - mysql

I would like to count how many rows there is of the same order_id in the table refnumbers
So it should output:
There's 5 rows with the order_id 123
There's 9 rows with the order_id 124
There's 18 rows with the order_id 125
There's 2 rows with the order_id 77
It's the column order_is that it should counter after in the table refnumbers
I dont really know how to do this without specifically mention a order_id and then do a loop through them in php.

You need to use group by on the column that you want to get the count for.
This does not work
SELECT count(*) as rowcount
FROM refnumbers
Will give you a single row with all rowcounts
Count per distinct order_id
SELECT order_id, count(*) as rowcount
FROM refnumbers
GROUP BY order_id /*WITH ROLLUP*/
ORDER BY order_id
Will give you the count per distinct order_id.
If you want to get the total count as well uncomment the with rollup part.

Related

How to get maximum value after groupby mysql

Im trying to get a maximum value after im preforming a groupby clause.
select count(*) as count, store_id from sakila.customer
group by store_id
my output for this code is:
count
store_id
326
1
273
2
how can i get a max value from the count column? i tried several things and nothing seems to work.
Just order your results and limit them to 1:
select count(*) as count, store_id from sakila.customer
group by store_id
order by count desc
limit 1

Aggregated row count differences between tables

I have two MySQL tables A and B both with this schema
ID
entity_id
asset
asset_type
0
12345
x
1
..
.........
.....
..........
I would like to get an aggregated top 10/50/whatever entity_ids with the largest row count difference between the two tables. I think I could do this manually by just getting the highest row count by entity_id like so
select count(*), entity_id
-> from A
-> group by entity_id
-> order by count(*) desc;
and just manually comparing to the same query for table B but I'm wondering if there's a way to do this in just one query, that compares row counts for each distinct entity_id and aggregates the differences between row counts. A few notes
There is an index on entity_id for both tables
Table B will always have an equivalent or greater number of rows for each entity_id
Sample output
entity_id
difference
12345
100
3232
75
5992
40
and so on
for top 10/50
Aggregate in each table and join the results to get the difference:
SELECT a.entity_id, b.counter - a.counter diff
FROM (SELECT entity_id, COUNT(*) counter FROM A GROUP BY entity_id) a
INNER JOIN (SELECT entity_id, COUNT(*) counter FROM B GROUP BY entity_id) b
ON a.entity_id = b.entity_id
ORDER BY diff DESC LIMIT 10

select all rows with lowest product_id

i have this table and i want to select all rows with product id 105. i can easily do this if i know product id and put in where clause but i dont know product id and it increases once this id work is completed it will delete these rows and search for next lowest product id.
with order bt product_id i can do but how to ensure that all rows are listed. if i will give 100 limit then first 10-15 may be lowest after that it will start listing next higher product_id rows
here is table
id name product_id some_columns
1 dff 105 gfff
2 fg 109 ffgfgf
3 tt 106 gttytt
4 tt 105 trtr
5 trr 112 trrrt
6 rrr 111 rttttr
7 ttyt 108 ttrtrtr
8 rrrr 105 rrerer
SELECT id, name, product_id, some_columns
FROM table_name
WHERE product_id = (SELECT MIN(product_id) FROM table_name)
here you can see that lowest product_id is 105 but i dont have any control on how many times it will appear in table. in some case it may be 10 rows and in some case it may be 250 rows.
so order by product_id will not work. as giving limit will list all id initial few rows may be 105 but after than when all 105 rows are listed it will start listing all rows which is higher than 105
the best solution would be if i could use where product_id=105 but my bad luck i dont have any control on product id so cant use product id in where clause. next big problem is i want to use it efficiently so we have indexed product_id column
i was exploring min value option but i am highly doubtful about its efficiency and probable affect on mysql
so any help will be great
You can try something like
SELECT id, name, product_id, some_columns
FROM table_name
WHERE product_id = (SELECT MIN(product_id) FROM table_name)
As far as I understood you want to select all the rows that match the minimum product_id. Be it 101, 102 or whatever, it's uncontrollable, so you get the minimum product_id in the row and then you select the rows that has the current minimum product_id.
You could try using a subquery for min_id group by product
select m.*
from table_name m
inner join (
select min(id) min_id, product_id
from table_name
group by product_id
) t on t.min_id = m.id

How do I pull last 5 entry from a from a table through SQL query?

I have a table with data entry like below. I want to select the last 5 rows all the time when new data will be inserted. So, product_id 2-6 will be selected in here. But when I will enter a new product, id 3-7 will be selected and afterwards.. What will be SQL query for this? I am newbie. Any help will be greatly appreciated. Thanks in advance.
product_id product_name product_price
------------------------------------------
1 Phone 120
2 Chips 2
3 Television 300
4 PC 400
5 Radio 50
6 Watch 10
SELECT *
FROM Tab
ORDER BY product_id DESC
LIMIT 5
You can use the below query assuming that "product_id" is incremental as the new rows get added.
Select * from table_name order by product_id DESC limit 5.
Explanation:
The query will sort the rows based on product_id in descending order and limit 5 will Limit the output to 5 from first row. So even a new row is added the query will give you the last five rows.
Hope it helps.
Thanks
You could use the LIMIT keyword, which limits the results of a query to a number of rows. Combining it with the ORDER BY means that it's sorted in the right order.
SELECT product_id, product_name, product_price
FROM yourtable
ORDER BY product_id DESC
LIMIT 5;
If your IDs are always sequential (no records get deleted) and you insist on having your record sorted in ascending order you can try:
SELECT product_id, product_name, product_price
FROM table
WHERE product_id > (SELECT MAX(product_id)-5 FROM table);
This will work on MySQL

SQL query distinct with additional select

I need to perform a query in MySQL that returns distinct values for product_id but also I need to select and return 'id' field which is in that particular table.
This query will return distinct product_id's without id:
SELECT DISTINCT product_id FROM orders_cart
This query will use distinct on both fields which and I want to use it on product_id and see the id
SELECT DISTINCT id, product_id FROM orders_cart
It would be quite easy to do on pgsql but I have no idea how to do this on mysql.
Your query is not well-defined: Consider this table
id product_id
1 1
2 2
3 1
4 2
What should your query result be? If you mean
id product_id
1 or 3 1
2 or 4 2
you are in the land of non-deterministic queries.
What you could do is
SELECT MIN(id), product_id FROM orders_cart GROUP BY product_id
which would deterministically produce
id product_id
1 1
2 2
Thats my final code:
The most important bits for this issue were line 1,2 and 4 :)
GROUP BY did the trick :)
SELECT orders_cart.id, product_id, order_id
FROM orders_cart
LEFT JOIN orders_order ON orders_cart.order_id=orders_order.id
WHERE orders_order.status='Wysłano'
GROUP BY orders_cart.product_id