In a table i have some of column have duplicate values i want to retrieve unique values from my table i used SELECT DISTINCT column_name FROM table_name query and i got unique columns but my problem is i also want id of anyone of the duplicate value how can i retrieve that from using a single query ?
Eg
+----+------+------+
| id | name | po |
+----+------+------+
| 1 | some | 2 |
| 2 | xyzs | 3 |
| 3 | frth | 2 |
| 4 | lopd | 3 |
| 5 | gtry | 2 |
+----+------+------+
i want to find unique po and any one of its id
Output
some thing like this
po - 2 id - ( any of 1,3,5)
po - 3 id - ( any of 2 or 4)
Just group them and get the max id or the min.
SELECT max(id), po FROM table_name group by po
try this:
SELECT MIN(id) id, po
FROM table_name
GROUPB BY po, id
Don't quote me on this, but you might be able to do something like:
SELECT GROUP_CONCAT(id) FROM table_name GROUP BY po
If you don't care which id you will get, then:
SELECT po,id FROM table GROUP BY po
If you wish to get first/last of the ids with that same po, you can add MIN(id)/MAX(id) as well:
SELECT po,MIN(id) as id FROM table GROUP BY po
You can also have all the ids for that po:
SELECT po,GROUP_CONCAT(id) as ids FROM table GROUP BY po
Related
How to query for erase the view below?
+-------------------+------------+
| Order_id | Weight |
| 20 | 4 |
| 21 | 5 |
| 22 | 2 |
| 22 | 2 |
+-------------------+------------+
To be like this:
+-------------------+------------+
| Order_id | Weight |
| 20 | 4 |
| 21 | 5 |
| 22 | 2 |
| 22 | |
+-------------------+------------+
When displaying results but not entered into the database.
A simple way is:
select DISTINCT order_id, weight from xyz
UNION
select order_id, null from xyz
group by order_id, weight
having count(*) > 1
Order by weight desc;
The 1st select statement will display all the unique values and 2nd one will retrieve only the repeated values.
In your required output table, it seems like you want to display all the non-repeated rows and the 1st column value of repeated rows but not 2nd column value. The above query will allow you to do that.
OK, here is how to do it:
SELECT
Order_id,
Weight,
if(#order_id = Order_id, '', Weight) as no_dup_weight,
#order_id := Order_id as dummy
FROM Table1
ORDER BY Order_id asc;
You basically need to check to see if the previous Order_id is the same as the current, and if they are, output an empty field.
Here is an SQLFiddle demonstrating the solution.
Do you actually need 2 rows for the dupes? Can't you just use the DISTINCT clause as per http://www.mysqltutorial.org/mysql-distinct.aspx
Or is it important to know what has duplicates. In which case you should look into the GROUP BY clause
I have a table like
+------+----------+
| id | location |
+------+----------+
| 1 | TVM |
| 2 | KLM |
| 3 | EKM |
+------+----------+
And I have an array of id like [1,2,1,3,1]. I need to get the result as
+------+----------+
| id | location |
+------+----------+
| 1 | TVM |
| 2 | KLM |
| 1 | TVM |
| 3 | EKM |
| 1 | TVM |
+------+----------+
I am already tried WHERE IN like conditions but no luck.
A where statement cannot multiply the number of rows. It can only filter rows out. You want a join:
select tl.*
from tablelike tl join
(select 1 as n union all select 2 union all select 1 union all
select 3 union all select 1
) n
on tl.id = n.n;
Note: if you are already generating the list via a query or from a table, then use that for the query rather than passing the list out of the database and then back in.
You could also return this result with a query like this; this uses a separate SELECT to return each occurrence of row with id=1.
( SELECT id, location FROM mytable WHERE id IN (1,2)
ORDER BY id
)
UNION ALL
( SELECT id, location FROM mytable WHERE id IN (1,3)
ORDER BY id
)
UNION ALL
( SELECT id, location FROM mytable WHERE id IN (1)
ORDER BY id
)
Following a similar pattern, the result could be obtained by combining the results from five SELECT statements, each returning a separate row. That would probably be a little simpler to achieve from a small array, e.g.
$glue = ") ) UNION ALL
(SELECT id, location FROM mytable WHERE id IN (";
$sql = "(SELECT id, location FROM mytable WHERE id IN ("
. implode($glue, $myarray)
. ") )";
Hello there, I have a schema like this, table name feeds
Where msg_id is unique and its the primary key of the table
| msg_id |commented|
| 1 | 10 |
| 2 | 10 |
| 3 | 10 |
| 4 | 21 |
I want to build a query that would select the last two rows
The output should go like this
| msg_id |commented|
| 3 | 10 |
| 4 | 21 |
In short the query should return the rows with msg_id which have a distinct commented value
Group by the column ment to be unique and select the highest id for every group
select max(msg_id) as msg_id, commented
from your_table
group by commented
Try this -
select max(msg_id), commented from your_table group by commented
SELECT * FROM feeds GROUP BY commented
Lets say i have a table in my database there looks like this:
|-------------------------|
| id | numbers |
|-------------------------|
| 1 | 1,3,5,7 |
| 2 | 2,4,6,8 |
| 3 | 1,2,3,4,5,6,7,6,8 |
|-------------------------|
I want to remove let us say 4 for all the rows that has the number 4 in the numbers column. What is the sql call to this? I'm using php and mysql.
To make it more understandable I use different table and column names. A better table design would be
users table
-------------------
id
name
other_columns
roles table
-------------
user_id
role_number
Example data:
users
--------------
id name
1 peter
2 tom
roles
----------
user_id role_number
1 1
1 3
1 7
2 2
2 8
Using this design you can now query for all roles a user has like this
select r.role_number
from users u
join roles r on u.id = r.user_id
where u.name = 'peter'
or if you already have the users ID then
select role_number
from roles
where user_id = 1
Using string functions you may try to write following query:
UPDATE
`table_name`
SET
`numbers` = TRIM(BOTH ',' FROM REPLACE(CONCAT(',', `numbers`), ',4', ''))
WHERE
FIND_IN_SET('4', `numbers`);
Test:
SELECT TRIM(BOTH ',' FROM REPLACE(CONCAT(',', '4,2,3,4,5,6,7,6,8,4'), ',4', ''));
Result:
2,3,5,6,7,6,8
I have two table name users and users_images. Both table have the value of userId. like
My user table
| userId | userName | user_address |
| 2 | John | CN-2, UK |
| 3 | Amit | India |
| 4 | David | Us |
| 5 | Shan | Canada |
.
.
...... and so on
| 125000 | Naved | Ukran |
**and my images table contain userid and Image name.
Now I want to merge ImageName field to user table without using any loop (I want to do it with single query (I have millions of records and I will have to do it many times to create temorary table) )
update users u
set
u.imageName = (
select imageName
from users_images i
where i.userid = u.userid GROUP BY u.userId )
you could use ON DUPLICATE KEY
for instance:
INSERT INTO table (a,b,c) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE c=c+1;
I think you can use Update for this like:
UPDATE Users
SET ImageName =
(SELECT ImageName
FROM UserImages
WHERE UserImages.UserID = Uers.UserID)
Please take a backup of your database first