I've been looking to see if there's a MySQL selector that would allow me to select by one column if another has a different value.
Example:
id name value
----------------
1 john 1
2 craig 1
3 john 2
So, what I'd be looking to do is select both rows 1 and 3 since they have the same name and the value has changed.
If this isn't possible, I can parse through all the results in code, but if MySQL can do this for me, even better!
Try this:
Select * from `table` where Name in (
select name from `table` Group by Name having Count(*)>1)
Inner select Looks for names that exists more than one in your table and the outer select get the data for that name.
You could select all the names that have more than value, and then all the rows with those names:
SELECT *
FROM mytable
WHERE name IN (SELECT name
FROM mytable
GROUP BY name
HAVING COUNT(*) > 1)
this may work;
select * from table a
where exists
(select b.id from table b
where a.name = b.name and a.value <> b. value)
Related
Hey guys I have a long table in my Database and i want select all records that have the same id and parent_id.
id
name
parent_id
2
lorem
2
Second case:
In the second case there are ids and parent_ids in different rows.
Thanks in advance.
SELECT ID,
PARENT_ID
FROM TABLENAME
WHERE ID=PARENT_ID
I think you can use this structure:
SELECT [column list] FROM [tablename] WHERE id=parent_id
you simply need to put an = sign for the column that you want to have the same values. Passing this condition to the where clause will filter the rows to show only the ones where the two columns are equal.
select * from <your_table_name> where id = parent_id
<your_table_name> = pass your table name
for your case 2 where you want the rows that matches the id = parent_id but they are not in the same row:
you need a self-join
SELECT
*
FROM
<table_name> as t1
INNER JOIN <table_name> as t2
on t1.id = t2.parent_id
if in different tables:
SELECT * FROM table1 WHERE parent_id in (SELECT id FROM table2)
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).
If the table shows like below.
Number Name
1 A
2 A
3 B
4 C
5 D
So for the example, I want to get the A.
Should I use join here?
You can use group by simply to get the same.
Select Name from [User] group by Name having COUNT(Name ) > 1
Try this:
SELECT * FROM (
SELECT Name,COUNT(*) AS[sum]
FROM[yourtable]
GROUP BY Name
) X
WHERE X.[sum] > 1
Try this code,
SELECT DISTINCT names
It will show unique value of table has more than a value.
Hope this help !
You can use group by and having to get your required result. You can use also a count to see the how many times same name there
SELECT COUNT(name) AS total,name FROM tablename GROUP BY name HAVING COUNT(name) >2
you can use inner join
SELECT a.* FROM tbname AS a
INNER JOIN (
SELECT NAME FROM tbname GROUP BY NAME
HAVING COUNT(NAME)>1
) AS b ON a.name=b.name
I'm trying to figure out the best/easiest way to number duplicate rows accordingly.I have a set of data that I am uploading to the database table. I have uploaded it, and auto incremented it, now I want to generate the order_id in the fashion shown in my question. For example
----ID-------NAME-----------ORDER_ID----------
1 Bob Smith 1
2 Steve Jones 2
3 Bob Smith 1
4 Billy Guy 3
5 Steve Jones 2
----------------------------------------------
I was thinking I could use a statement such as select NAME from table where name= duplicate_name but I can't seem to figure out how I would realistically go about that, much less then enter the appropriate ORDER_ID afterwards. Is there an easy way to do this ?
You could do something like this:
SELECT
A.ID,
A.NAME,
(SELECT TOP 1 T.ID
FROM table AS T
WHERE T.NAME = A.NAME
ORDER BY T.ID) AS ORDER_ID
FROM table AS A
If your database engine does not support the TOP keyword, but does support the LIMIT keyword, you may be able to do this:
SELECT
A.ID,
A.NAME,
(SELECT T.ID
FROM table AS T
WHERE T.NAME = A.NAME
ORDER BY T.ID
LIMIT 1) AS ORDER_ID
FROM table AS A
SELECT t.*, t1.order_id
FROM (
SELECT name, #rownum := #rownum + 1 AS order_id
FROM (
SELECT name, min(id) AS min_id
FROM tbl
GROUP BY name
) t0
, (SELECT #rownum := 0) r
ORDER BY min_id
) t1
JOIN tbl t USING (name);
In the subquery t0 aggregate all names and compute the minimum id per name. You seem to want to number names after first appearance according to id. This is subtly different from just adding dense_rank() like I commented.
Simulating the basic window function row_number(), tag a running number to each name according to this order in t1.
Join back to the base table.
I have a problem ordering my results correctly when using the group by. It seems to show the first entry in the database instead of the most recent in the group.
Example:
id(autoincrement) | name
1 | anne
2 | james
3 | anne
4 | brad
As you can see I have "anne" entered multiple times which is why I am using the group by. I would then like it to display the "anne" that is the most recent, which would be the entry "3". Instead it displays the first "anne"(1)
My query
"Select * FROM TABLE GROUP BY name ORDER BY id DESC
Any help would be greatly appreciated.
The problem is that you're selecting all the fields (using * is seldom a good idea) so each row is unique therefore there is nothing to group on.
Try:
SELECT
Name,
MAX(ID)
FROM
TABLE
GROUP BY
Name
A possible solution:
SELECT id, name
FROM TABLE
WHERE id IN (SELECT MAX(id) FROM TABLE GROUP BY name)
Try this; it will work:
SELECT * FROM TABLE
INNER JOIN (SELECT MAX(id) AS id
FROM TABLE
group by name)
ids ON TABLE.id = ids.id
Try
SELECT DISTINCT name
FROM table
ORDER BY id DESC
Building on another anwer already provided, this SQL will avoid having to GROUP BY Name:
SELECT
DISTINCT Name,
MAX(ID)
FROM
TABLE