SQL query distinct with additional select - mysql

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

Related

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

Guidance required for sql query

I have a database with one table as shown below. Here I'm trying to write a query to display the names of medication manufactured by the company that manufactures the most number of medications.
By looking at the table we could say the medication names which belongs to the company id 1 and 2 - because those company manufactures the most medication according to this table, but I'm not sure how to write a query for selecting the same i said before.
ID | COMPANY_ID | MEDICATION_NAME
1 1 ASPIRIN
2 1 GLUCERNA
3 2 SIBUTRAMINE
4 1 IBUPROFEN
5 2 VENOFER
6 2 AVONEN
7 4 ACETAMINOPHEN
8 3 ACETAMINO
9 3 GLIPIZIDE
Please share your suggestions. Thanks!
Several ways to do this. Here's one which first uses a subquery to get the maximum count, then another subquery to get the companies with that count, and finally the outer query to return the results:
select *
from yourtable
where companyid in (
select companyid
from yourtable
group by companyid
having count(1) = (
select count(1) cnt
from yourtable
group by companyid
order by 1 desc
limit 1
)
)
SQL Fiddle Demo
This Query might work. I have not tested but the logic is correct
SELECT MEDICATION_NAME
FROM TABLE where
COMPANY_ID=(SELECT
MAX(counted)
FROM ( SELECT COUNT(*) AS counted FROM TABLE ) AS counts);

Is there anyway to perform a Count() when using Min()?

The statement below is to get all non-duplicate IDs from the products table. Is there a way I can get the total count of rows that are outputted by this sql statement?
select min(product_id) from products
where market_code = 'germany'
group by product_code
sample table data:
product_id market_code product_code
1 uk AAA
1 uk AAA
1 uk AAA
2 germany BAA
2 germany BAA
3 uk BAA
Thanks
You can simply do this:
SELECT COUNT(*)
FROM
(
select min(product_id) from products
where market_code = 'germany'
group by product_code
) AS t;
Actually your query is not what you said "The statement below is to get all unique/non-duplicate IDs from the products table." , It will select lowest product_id for product_code , not unique , for example if there are product_id - 2 and product_id - 3 for product_code A , it will only return product_id - 2 , it is a minimum value not unique , if you want unique you could do this way
SELECT product_code,product_id,count(*)
FROM TABLE
GROUP BY product_code,product_id
if you want just unique/non-duplicate ID-s and their count you can select with this query
SELECT product_id,count(*)
FROM table
GROUP BY product_id
The statement:
select min(product_id)
from products
where market_code = 'germany'
group by product_code;
is going to have as many rows as there are product code values in Germany. If you assume that a given product id never has two codes (which is true of your sample data), then the following counts the products in Germany without using product id at all:
select count(distinct product_code)
from products
where market_code = 'germany';

Condition for counting distinct rows in an SQL query

I have a table in a MySQL database with an ID column. This is not a key of the table and several rows can have the same ID.
I don't really know SQL but I already figured out how to obtain the number of distinct IDs:
SELECT COUNT(DISTINCT ID) FROM mytable;
Now I want to count only those IDs which appear more than 2 times in the table.
So if the ID column contains the values
3 4 4 5 5 5 6 7 7 7
the query should return 2.
I have no idea how to do this. I hope someone can help me!
Btw, my table contains a huge number of rows. So if there are several possibilities I would also be happy to know which solution is the most efficient.
Try this:
SELECT COUNT(ID) FROM (
SELECT ID FROM mytable
GROUP BY ID
HAVING COUNT(ID) > 2) p
select count(*) from
(select count(id) as cnt,id from mytable group by id) da
where da.cnt>2
The inner query will give you how many elements does each id have. And the outer query will filter this.
SELECT
COUNT(ids)
FROM
(SELECT
COUNT(ID)AS ids
FROM
mytable
GROUP BY
ID
HAVING
ids>2
)AS tbl1
Updated :
SELECT count(ID)
FROM (
SELECT ID FROM mytable
GROUP BY ID
HAVING count(ID) > 2
) p
should do what you need

SQL Grouping and counting using existing row

Lets say we have a table Requests with structure:
user_id
product_id
count
With entries like:
1 1 5
1 2 6
1 1 3
2 1 7
2 1 3
2 2 5
I want to count how much of each product each user has.
And get output like this:
1 1 8
1 2 6
2 1 10
2 2 5
Use GROUP BY with the SUM aggregate function:
SELECT user_id, product_id, SUM(count) AS total
FROM Requests
GROUP BY user_id, product_id
Your query will look something like this:
SELECT user_id, product_id, SUM(`count`)
FROM Requests
GROUP BY user_id, product_id
I wouldn't name a field "count" if I had the choice, as it's a SQL function and could cause weird naming conflicts down the road.
You can find a tutorial on how to use GROUP BY here:
SQL GROUP BY statement
You can also find specific information on the syntax and use of GROUP BY in MySQL in the MySQL Manual:
MySQL SELECT Syntax
MySQL GROUP BY Functions And Modifiers