How to query distinct records in mysql - mysql

Hi I want to query unique records using distinct in mysql but the problem is that it only returns a single column, how can I make it return all?
Here's my initial query:
SELECT distinct(country_name)
FROM HST_LOCATION
WHERE person_id='1897'
ORDER BY date_updated DESC
group by country_name

The GROUP BY clause makes that column distinct, there's no need to use the DISTINCT modifier. Just select all the columns and you should get what you want.
SELECT *
FROM HST_LOCATION
WHERE person_id = '1897'
GROUP BY country_name
ORDER BY date_updated DESC

Related

how to get first value of duplicate value in mysql

I have a table where I am having duplicates value also.
From that table, I want to get the first value of duplicate values via an order by id desc.
I am using below query to find count
select product_sku, quantity
from catalog_product_store_inventory
where ax_store_id=999
ORDER BY id DESC;
From this query, I get the all duplicates value.
I hope I made my query clear.
I am very new to MySQL.
What means duplicates in your case? Duplicates according to what product_sku or both product_sku,quantity - this should be used in GROUP BY clause:
SELECT product_sku,quantity
FROM catalog_product_store_inventory c
JOIN (
SELECT MAX(id) id
FROM catalog_product_store_inventory
GROUP BY product_sku
) m ON c.id = m.id
ORDER BY id DESC means that you want last ID from group and this one is MAX.

MySQL Row more appear with multiple row same result

I have a query with a count with some group by, and I want to get the greater count. I can do with an order by and limit 1, but I have multiple results with the same count and then does not work for me.
How do I solve this problem?
With MySql 8+, you can use CTE to get the maximum count, and then retrieve all records with a count equal to the maximum count.
However, since CTE is not available in MySql 5.6, you'd need to use a sub-query to get the maximum count, and then write the main query which compares the count of each record to the maximum count retrieved in the subquery.
Here is a query I wrote. Maybe it's not the most efficient solution, but it gets the desired result.
SELECT
group_id, COUNT(record_id) c
FROM
table_name
GROUP BY group_id
HAVING c IN (
SELECT
MAX(sub_query.c)
FROM
(SELECT
group_id, COUNT(record_id) c
FROM
table_name
GROUP BY group_id) AS sub_query
)
If you are not going to do this using window functions, you can do:
SELECT group_id, COUNT(*) as cnt
FROM table_name
GROUP BY group_id
HAVING cnt = (SELECT COUNT(*)
FROM table_name
GROUP BY group_id
ORDER BY COUNT(*) DESC
LIMIT 1
) ;

MySQL Subquery from ASC order to DESC order

select *, sum(sales_qty) as total_qty
from sales_details
left join sales on sales.salesid=sales_details.salesid
where month(sales.sales_date)='$m'
group by productid order by total_qty asc limit 2, 4
I have that sql that is sorted in ascending order. Now I want the results to be sorted in descending order. I have tried using subquery but doesn't work for me. Any help or suggestion is appreciated.
This is the sql that I have tried:
select * from (
select *, sum(sales_qty) as total_qty
from sales_details
left join sales on sales.salesid=sales_details.salesid
where month(sales.sales_date)='$m'
group by productid
order by total_qty asc
limit 2, 4
) as sub
order by sum(sales_qty) desc
Your query with the subquery should end
... ORDER BY sales_qty DESC
Why? When you say ... ORDER BY SUM(sales_qty) DESC you're converting the outer query into an aggregate query. Because that outer aggregate query has no GROUP BY clause, it necessarily has a one row result set.
The outer query treats the inner query as if it were a table, a virtual table. That virtual table has a column named sales_qty. It is the value by which you want to order.
Pro tip: Don't use SELECT * in aggregate queries. You're misusing the notorious nonstandard MySQL extension to GROUP BY, which means your result set contains unpredictable values. Read this. https://dev.mysql.com/doc/refman/5.7/en/group-by-handling.html

MySQL 5.7 How to do GROUP BY with sorting?

Similar to this issue: MySQL 5.7 group by latest record
I'm not sure how to do this properly in 5.7. Also with possibility of 2nd sort column. Working query in 5.6 that I'm trying to replicate in 5.7:
SELECT id FROM test
GROUP BY category
ORDER BY sort1 DESC, sort2 DESC
id is not always the highest, so MAX(id) does not work.
Looking into the link above, the solution for single sort should be:
SELECT t1.*
FROM test t1
INNER JOIN (
SELECT category, max(sort) AS sort FROM test GROUP BY category
) t2 ON t2.category = t1.category AND t2.sort = t1.sort
But how will it work with 2 sorting?
You are using GROUP BY the wrong way.
Think of group by as a way to separate data row into different groups. Each group has multiple rows, based on the value of group by column.
Once you get those groups, selecting table columns (as in: select *) is like picking any row from that group randomly. This is not helpful nor useful.
Usually once we group records (or rows), we need to find meta information about those records. For example: get us the count of records in that group (as in: select count(*)), or the sum of values of a specific column in that group (as in: select sum(price)), or get the min, max or avg values.
So in a nutshell, when you use group by you should use on of the aggregation functions with it, otherwise it's not going to do you any good.
Why don't you have the ORDER BY at your outer query, instead?
SELECT *
FROM (
SELECT 100 AS id, 1 AS category, NULL AS sort
UNION
SELECT 200 AS id, 1 AS category, 2 AS sort
) dt
GROUP BY category
ORDER BY sort DESC;
It seems that what happened to the data when it was grouped, it took the first data while neglecting the ORDER BY DESC. On your first query, it ordered descending first then group by took the first record which is 200. And yes, this shouldn't be the way you should use GROUP BY. It is used in conjunction with aggregate functions.
when you select a column in a group by query that is not one of the columns you are grouping by, (ie, your id) you have no control over the value unless you use another aggregate function. If you want to sort, use MIN or MAX:
SELECT MAX(id), category, FROM `test2`
GROUP BY category; -- always returns 200
SELECT MIN(id), category, FROM `test2`
GROUP BY category; -- always returns 100

How to select the last record from MySQL table using SQL syntax if only a condition is meet

I have four fields in my db namely
Id(auto increment),
dept_id,
mat_code,
topic
I want to retrieve the last record in the database if a condition is meet. Am using dept_id for the condition.
The normal method is to order the result in DESC order and LIMIT the result set to 1 row
SELECT Id, dept_id, mat_code, topic
WHERE dept_id = 'something'
ORDER BY Id DESC
LIMIT 1
What is the condition? For what you want, I think a subquery is necessary:
SELECT t.*
FROM (SELECT t.*
FROM t
ORDER BY Id DESC
LIMIT 1
) t
WHERE dept_id = ??;
The subquery returns the last row (based on id). The outer WHERE determines if conditions are true.