MYSQL distinct query - mysql

This is my example of my table:
id | name | foreign_id |
-------------------------
1 a 100
2 b 100
3 c 100
4 d 101
5 a 102
6 b 102
7 c 102
I would like to get the distinct file with the latest foreign_id (bigger number but not necessarily biggest).
In this example, it would be row with id 4,5,6,7. Anyone has any idea? Thank you very much!

Could you try something like below :-
SELECT Id,Table1.Name,Table1.Fid FROM Table1 INNER JOIN
(SELECT Name,Max(FId) AS FId FROM Table1 Group By Name)
Table2 ON Table1.FId=Table2.FId AND Table1.Name=table2.Name
This works in Sql Server atleast. Please check in MySQL. Sorry, I dont have MySQL to test this.

Sounds like you just want this:
SELECT name, MAX(foreign_id)
FROM table
GROUP BY name;
If you need the ID (I'm guessing you won't, since name and foreign_id should probably be unique, making the ID column unnecessary), I think MySQL will allow you to get that by just adding that column to the SELECT list - although this is non-standard SQL. If you want standard SQL, then you want something like Ashish wrote.

SELECT
*
FROM
table_name
WHERE
foreign_id > 100

You could do something like:
select *
from table_name
where max_id_you_want = (select max(id_you_want) from table_name)

SELECT * FROM table
GROUP BY name
having MAX(foreign_id);

Related

GROUP BY inverse (mysql)

Is there any way to get the inverse of a group by statement in mysql? My use case is to delete all duplicates.
Say my table looks like this:
ID | columnA | ...
1 | A
2 | A
3 | A
4 | B
5 | B
6 | C
I want my result set to look like this:
ID | columnA | ...
2 | A
3 | A
5 | B
(Essentially this finds all duplicates leaving one behind. Could be used to purge all duplicate records down to 1, or to perform other analysis later).
One way is to take all but the first id for each value of ColumnA:
select t.*
from t
where t.id > (select min(t2.id) from t t2 where t2.columnA = t.columnA);
Your result seems
select max(id), columnA group by columnA
This should perform a lot better then inner select based queries.
SELECT
*
FROM
TABLE
QUALIFY
RANK() OVER (partition by columnA order by ID ASC ) = 1
EDIT : This apparently wont work in MySQL. Guess the only answer is to by a oracle license - or use another answer. ;)
I realized my own solution based on #scaisEdge response before he edited it. In need the opposite of my group by, so using a subquery:
SELECT * FROM mytable WHERE ID NOT IN (SELECT ID FROM mytable GROUP BY columnA);
I am confident this will help.
create table test.temptable select distinct * from YourTable;
truncate YourTable;
insert into YourTable select * from test.temptable ;

Find which values are not in table

Simple question, but I'm drawing a blank. Any help is appreciated.
I have a table of ids:
-------
| ids |
-------
| 1 |
| 5 |
| 7 |
-------
Except the actual table is thousands of entries long.
I have a list (x), not a table, of other ids, say 2, 6, 7. I need to see which ids from x are not in the ids table.
I need to get back (2,6).
I tried something like this:
SELECT id FROM ids WHERE id IN (2,6,7) GROUP BY id HAVING COUNT(*) = 0;
However, COUNT(*) returns count of retrieved rows only, it doesn't return 0.
Any suggestions?
Create a temporary table, insert the IDs that you need into it, and run a join, like this:
CREATE TEMPORARY TABLE temp_wanted (id BIGINT);
INSERT INTO temp_wanted(id) VALUES (2),(6),(7);
SELECT id
FROM temp_wanted t
LEFT OUTER JOIN ids i ON i.id=t.id
WHERE i.id IS NULL
Try something with "NOT IN" clause:
select * from
(SELECT 2 as id
UNION ALL
SELECT 6 as id
UNION ALL
SELECT 7 as id) mytable
WHERE ID not in (SELECT id FROM ids)
See fiddle here

Mysql Return Duplicates Of Certain Field With Full Row

I would like to have a mysql select query that will select all from a table and also include a count of duplicates that exist in the table for a specific field. Here is a table and the results I would like.
TestTable
Id Name Dogs
1 Eric 1
2 Dave 2
3 Chris 4
4 Eric 3
I would like to have the following results returned, based on a search for records that have duplicate names.
Id Name Dogs
1 Eric 1
4 Eric 3
select * from your_table
where name in
(
select name
from your_table
group by name
having count(*) > 1
)
Try This
SELECT
a.Id,a.Name,a.Dogs,
(SELECT count(b.Name)
FROM TestTable b
WHERE b.Name=a.Name) as totcount
FROM TestTable a GROUP By a.Name;
This will work.

JOIN 5 Database in MYSQL

I have 5 Database, Let say their name is A B C D E
All database have the same table / structure / field
I want to get result from 5 database using table SMSOutgoing and the field is uid
It look like this :
SELECT * OR JOIN 5 database A B C D E F
FROM `table` SMSOutgoing
WHERE uid = 1
Not all the database have uid=1, it need to display which database have the result
I run SMS Gateway, each phone / 1 number using 1 database, thats why there is so many different database.
I spent hours to solve it but always error, I think i follow the wrong guide (JOIN multiple table in 1 database)
I'm Lost, please Help and Thank You
Sounds like you want to list the databases out that contain uid = 1 in the SMSOutgoing table. If so, you should be able to use UNION:
SELECT DISTINCT 'DatabaseA' WhichDb
FROM DatabaseA.SMSOutgoing
WHERE uid = 1
UNION
SELECT DISTINCT 'DatabaseB' WhichDb
FROM DatabaseB.SMSOutgoing
WHERE uid = 1
UNION
...
UNION
SELECT DISTINCT 'DatabaseF' WhichDb
FROM DatabaseF.SMSOutgoing
WHERE uid = 1
I used DISTINCT in case you could have multiple uid in the same table -- that may be unnecessary.
EDIT: From your comments, it sounds like you just want the results:
SELECT *
FROM DatabaseA.SMSOutgoing
WHERE uid = 1
UNION
SELECT *
FROM DatabaseB.SMSOutgoing
WHERE uid = 1
UNION
...
UNION
SELECT *
FROM DatabaseF.SMSOutgoing
WHERE uid = 1
You may need to use UNION ALL if you might have duplicates...

How to get distinct record from mysql table?

I have a table student like this
id | name | zip
1 | abc | 1234
2 | xyz | 4321
3 | asd | 1234
I want to get all records but zip code should not be repeated. So In case of above table records, record No 1 and 2 should be fetched. Record No. 3 will not be fetched because it has a zip code which is already in record No. 1
SELECT DISTINCT fieldName FROM tableName;
The following query will only select distinct 'zip' field.
SELECT DISTINCT zip FROM student;
SELECT * FROM tableName GROUP BY fieldName;
The following query will select all fields along with distinct zip field.
SELECT * FROM student GROUP BY zip;
TRY
SELECT DISTINCT(zip),id,name FROM student;
OR
SELECT * FROM student GROUP BY zip;
Altough in MySQL you can get away with:
SELECT *
FROM student
GROUP BY zip
I would choose:
SELECT *
FROM student t
JOIN
( SELECT MIN(id) AS minid
FROM student
GROUP BY zip
) AS grp
ON grp.minid = t.id
Since presumably the other columns are of some interest....
SELECT y.*
FROM yourTable y,
(SELECT MIN(y2.id)
FROM yourTable y2
GROUP BY y2.zip) ilv
WHERE ilv.id=y.id;
(or you could use the max-concat trick)
update
Oracle have now removed the max concat trick from the linked page - but it is described elsewhere on the internet
Try Using
Select Distinct(zip),id,name group by zip;
Is there any problem if I use as this below?
select distinct zip,name,id from student;
select id, name, distinct(zip) from student;