get all unique records and their corresponding column - mysql

Is it possible to get all unique records as well as their corresponding column in a database?
something like:
SELECT DISTINCT *
FROM table_name
?where?
I want to get all unique records with their corresponding column.
I tried:
SELECT distinct(column_name), other_column
FROM table_name
?where?
I still get duplicate records.
I tried:
SELECT distinct(column_name)
FROM table_name
?where?
I get unique records but incomplete column. How can I get all unique records w/ their column?

Are you looking for something like this?
SELECT t.*
FROM
(
SELECT MIN(pk_id) pk_id
FROM table_name
GROUP BY fk_id
) q JOIN table_name t
ON q.pk_id = t.pk_id
Here is SQLFiddle demo
In Postgres you can use DISTINCT ON
SELECT DISTINCT ON (fk_id) t.*
FROM table_name t
ORDER BY fk_id
Here is SQLFiddle demo

Related

from the sample database , How could I make a query where I could search all the region ( region id) where Kivuto Id is duplicated?

I need to fetch the 3 lines as highlighted in the result with green i.e separate region id but same kivuto id.I need to rectify such products so that I could correct the kivuto id's
Try this.
select * from table_name
where kivuto_id in (
select email from table_name
group by kivuto_id
having count(*) > 1
)
You can refer to this as well: Find rows that have the same value on a column in MySQL
You can simply use exists:
select t.*
from t
where exists (select 1
from t t2
where t2.kivuto_id = t.kivuto_id and
t2.region_id <> t.region_id
);
For performance, you want an index on (kivuto_id, region_id).

Get distinct rows along with sum of field

My table structure is like this shown in below URL:
http://tinypic.com/r/sm3xbb/8
I want to write a MySQL query to get sum of count field of all such rows which have same hash value but have distinct date value.
This can be done with Group by statement but I won't get all rows if I will perform group by on hash field.
Example: Rows with ID 1,4 and 8 have same hash value in table, so I want to retrieve all 3 rows along with SUM of count field.
Another way to do it with a subquery
SELECT t.*,
(
SELECT SUM(count)
FROM table1
WHERE hash = t.hash
) total
FROM table1 t
Here is SQLFiddle demo
You can try something like this
select t.*,t1.sum_count
from mytable t
join
(
select hash, sum(`count`) as sum_count
from mytable group by hash
) t1 on t.hash = t1.hash

Confused with UPDATE query. How to convert select to update with MySQL?

I have seen very similar if not same questions on here but my trials of trying to convert following query into an UPDATE statement failed.
SELECT table.* FROM table JOIN (
SELECT column, COUNT(*) AS rank
FROM table
GROUP BY column
) AS t USING (column) WHERE t.rank = 1
ORDER BY t.rank DESC
I want to update column of all results selected using the query above.
How can I convert this into an update statement?
Thank you.
This should do it:
update table
set column = 'somevalue'
where id in
(select id from (
SELECT table.* FROM table JOIN (
SELECT column, COUNT(*) AS rank
FROM table
GROUP BY column
) AS t USING (column) WHERE t.rank = 1) x)
not entirely sure but i think it's something like
update tblname set columname = value where tblname.columncompare = (select statement)
INSERT INTO table (id, value)
SELECT table.id, table.value
FROM table
JOIN (
SELECT column, COUNT(*) AS rank
FROM table
GROUP BY column
) AS t USING (column)
WHERE t.rank = 1
ORDER BY t.rank DESC
ON DUPLICATE KEY UPDATE value = VALUES(value)
Insert on duplicate to the rescue!
Basicly this allows you to do any SELECT as normal and then you prepend INSERT INTO and append ON DUPLICATE.
I guess that this query is made up, but what's the point of filtering and ordering the same column?

Overall Unique count from two tables in MySQL

I have two tables, both having column a device_id column that I want to count. For the purposes of demonstration, the schema looks like:
Table 1: 'id', 'save_val', 'device_id_major'
Table 2: 'id', 'save_val', 'location', 'device_id_team'
Table 1 could have many of the same 'device_id_major'.
I basically want to get the unique device_id's from both tables, then from that result set, get the count of unique device_id's (the same device_id can appear in both tables).
Is this possible in one query?
select distinct aa.device_id, count(*)
from(select distinct device_id from table1
union all
select distinct device_id from table2) as aa
group by device_id
order by device_id
Or something like... As I don't have the schema to hand, I can't fully validate it.
SELECT count(DISTINCT aa.id)
FROM (SELECT DISTINCT major_id AS id FROM `major`
UNION ALL
SELECT DISTINCT team_id AS id FROM `team`)
AS aa
This seems to do the trick.
You could use a query that takes the UNION of both tables, then SELECT the unique values.

Mysql select distinct

I am trying to select of the duplicate rows in mysql table it's working fine for me but the problem is that it is not letting me select all the fields in that query , just letting me select the field name i used as distinct , lemme write the query for better understading
mysql_query("SELECT DISTINCT ticket_id FROM temp_tickets ORDER BY ticket_id")
mysql_query("SELECT * , DISTINCT ticket_id FROM temp_tickets ORDER BY ticket_id")
1st one is working fine
now when i am trying to select all fields i am ending up with errors
i am trying to select the latest of the duplicates let say ticket_id 127 is 3 times on row id 7,8,9 so i want to select it once with the latest entry that would be 9 in this case and this applies on all the rest of the ticket_id's
Any idea
thanks
DISTINCT is not a function that applies only to some columns. It's a query modifier that applies to all columns in the select-list.
That is, DISTINCT reduces rows only if all columns are identical to the columns of another row.
DISTINCT must follow immediately after SELECT (along with other query modifiers, like SQL_CALC_FOUND_ROWS). Then following the query modifiers, you can list columns.
RIGHT: SELECT DISTINCT foo, ticket_id FROM table...
Output a row for each distinct pairing of values across ticket_id and foo.
WRONG: SELECT foo, DISTINCT ticket_id FROM table...
If there are three distinct values of ticket_id, would this return only three rows? What if there are six distinct values of foo? Which three values of the six possible values of foo should be output?
It's ambiguous as written.
Are you looking for "SELECT * FROM temp_tickets GROUP BY ticket_id ORDER BY ticket_id ?
UPDATE
SELECT t.*
FROM
(SELECT ticket_id, MAX(id) as id FROM temp_tickets GROUP BY ticket_id) a
INNER JOIN temp_tickets t ON (t.id = a.id)
You can use group by instead of distinct. Because when you use distinct, you'll get struggle to select all values from table. Unlike when you use group by, you can get distinct values and also all fields in table.
You can use DISTINCT like that
mysql_query("SELECT DISTINCT(ticket_id), column1, column2, column3
FROM temp_tickets
ORDER BY ticket_id");
use a subselect:
http://forums.asp.net/t/1470093.aspx