Mysql: Select all values alphabetically between two strings in a table - mysql

Okay let's say I have this in my table:
id
name
1
tom
2
anna
3
beatrice
4
robert
5
xavier
6
zoe
7
eustace
How can I select all ids of the names that are alphabetically sorted?
Say, select * from myTable where name "between" 'beatrice' and 'tom' order by name;
Should give me :
id
name
3
beatrice
7
eustace
4
robert
1
tom
Because in alphabetical order, those are in between 'beatrice' and 'tom'.

If you only want the ID's you need:
select id from myTable where name between 'beatrice' and 'tom' order by name;

Since these are strings, try this:
SELECT id
FROM tableName
WHERE nameField >= 'Landon' and nameField <= 'Peter'
ORDER BY nameField;
If you want to use the full name (first and last), you can try with that too.

Related

Retrieve only the first row from each duplicate id in MySQL

I want to retrieve only the first row from each duplicate id in a table.
The issue is that every other field except the id is unique, so that means I cannot "filter" the rows.
Here is my scientist table:
id
firstname
lastname
1
albert
einstein
1
albert1
einstein1
1
albert2
einstein2
1
albert3
einstein3
2
isaac
newton
2
isaac1
newton1
3
marie
curie
3
marie1
curie1
3
marie2
curie2
Note: The firstname and the lastname are irrelevant I just want to extract the first row of each id.
And below you can find my desired output:
id
firstname
lastname
1
albert
einstein
2
isaac
newton
3
marie
curie
I have tried to GROUP BY id but it is not working.
Also, I have tried to select the scientist table based on the distinct ids of the same table
SELECT * FROM scientist WHERE id IN (SELECT DISTINCT id FROM scientist)
But then I realised that my logic was incorrect.
Try it here enter link description here
select id,firtname,lastname
from(
select *, ROW_NUMBER() OVER(PARTITION BY scientist.id) c
from scientist) t
Where c = 1

MYSQL, How to compare ID with other column in the same table

I have a table in which I want to compare an ID column with another column in the same table.
This is my table
ID | Name | BossID
1 John 3
2 Max 4
3 Peter 4
4 Alex 5
For example I want to use
select * from mytable where ID = BossID
and I expected to get that Peter is the Boss of John and Alex is the Boss of Max and Peter, but when I use it this way, I dont get any info on the query...
Any idea to get it?
The expected query result are:
ID | Name | BossID
1 John 3
2 Max 4
3 Peter 4
seems like you want the list of employees who their boss exists in the list as well:
select *
from mytable
where bossid in (select id from mytable);
to get the boss name :
select t.ID, t.Name, b.Name as BossName
from mytable t
join mytbale b
on t.bossid = b.id
you can use left join to return all the employees in the list

MySQL Generate a list of names that appear in more than one column, grouped alphabetically

I need to create a digest of records which are grouped by names in a table. The problem is, my table has two columns for names and the same names can be in either column. The digest should list all names and the records in which they exist, regardless of which column.
I can see doing this by first generating a unique list of names from both columns, then scanning through the result to see if the names appear in either column. I want to know if MySQL can do this in one query.
Example, here's my table:
Id ColumnA Column B
1 Bill NULL
2 NULL Dennis
3 Adam Carl
4 NULL Adam
5 Adam Bill
6 Dennis NULL
7 Frank Bill
The result of the query (NULL sorts to the top)
NULL 4 NULL Adam
NULL 1 Bill NULL
NULL 2 NULL Dennis
NULL 6 Dennis NULL
Adam 5 Adam Bill
Adam 7 Adam Carl
Bill 1 Bill NULL
Bill 5 Adam Bill
Bill 6 Frank Bill
Carl 3 Adam Carl
Dennis 2 NULL Dennis
Dennis 6 Dennis NULL
Frank 7 Frank Bill
It would be nice if I could eliminate the NULL entries at the top of the list. I only put them there because everything I'm trying so far has them at the top. I'd rather the result looks like this:
Adam 5 Adam Bill
Adam 7 Adam Carl
Bill 1 Bill NULL
Bill 5 Adam Bill
Bill 6 Frank Bill
Carl 3 Adam Carl
Dennis 2 NULL Dennis
Dennis 6 Dennis NULL
Frank 7 Frank Bill
You need to do a union of ID, ColumnA with ID, ColumnB, then summarize and order the result.
Like so http://sqlfiddle.com/#!2/eeec6/5/0:
SELECT name, GROUP_CONCAT(DISTINCT id ORDER BY id) AS ids
FROM (
SELECT id, a AS name
FROM names
UNION
SELECT id, b AS name
FROM names
) AS t
WHERE name IS NOT NULL
GROUP BY name
ORDER BY name
This will give you one row per name, with a list of IDs (row numbers) containing that name.
Or you can simply display the name, id columns one at a time(http://sqlfiddle.com/#!2/eeec6/6/0):
SELECT name, id
FROM (
SELECT id, a AS name
FROM names
UNION
SELECT id, b AS name
FROM names
) AS t
WHERE name IS NOT NULL
ORDER BY name, id
Finally, if you want to show the whole row with each name, you can do this (http://sqlfiddle.com/#!2/eeec6/8/0):
SELECT name, n.id, n.a, n.b
FROM (
SELECT id, a AS name
FROM names
UNION
SELECT id, b AS name
FROM names
) AS t
JOIN names AS n ON t.id = n.id
WHERE name IS NOT NULL
ORDER BY name, n.id
One approach:
SELECT s.ColumnA AS digest_name
, s.ID
, s.ColumnA
, s.ColumnB
FROM mydata s
WHERE s.ColumnA IS NOT NULL
UNION ALL
SELECT t.ColumnB
, t.ID
, t.ColumnA
, t.ColumnB
FROM mydata t
WHERE t.ColumnB IS NOT NULL
ORDER BY 1, 3, 4
One query gets all the rows where ColumnA is not null, the second query gets the rows where Column B is not null. (This effectively omits rows where the digest name would have been NULL.)
The UNION ALL set operator combines the two sets of rows into a single set. (NOTE: We prefer the more efficient UNION ALL operator over the UNION operator, because it doesn't perform the extra work of identifying and removing duplicate rows, when that operation isn't required.)
The ORDER BY specifies that the rows are to be ordered by the digest name first, then by the names in ColumnA and ColumnB.
This produces ten rows of output, and includes row:
digest Id ColumnA ColumnB
-------- -- -------- --------
Adam 4 NULL Adam
It's not clear why this row is omitted from the resultset specified in the question. (That resultset in the question shows only nine rows.)
So, I'm not sure if I'm missing something about the requirement. It's entirely possible I've not understood the specification.

How to group by a field and return list of other field?

I have a table with columns id, user
I want to group by column id and show a list of users (comma separated) for each id.
In the final output I need to display:
user joe - id 1
users jim, mark, john - id 2
user dave - id 3
....
I'm getting error "Cardinality violation: 1242 Subquery returns more than 1 row" if I try this:
SELECT id, (SELECT distinct(user) FROM mytable b where a.id = b.id)
FROM mytable a
GROUP BY id
Since you are using MySQL, there is a built-in function for that which is the GROUP_CONCAT function. For example, you have a records like this:
ID User
1 Joe
2 Jim
2 Mark
2 John
3 Dave
If you try to run this query:
SELECT ID, GROUP_CONCAT(User) Users
FROM tableName
GROUP BY ID
you will then have a result like this:
ID Users
1 Joe
2 Jim, Mark, John
3 Dave
What you're looking for is the GROUP_CONCAT aggregate function:
SELECT id, GROUP_CONCAT(user) FROM table GROUP BY id

Delete partial duplicates from a MySQL table

I have a table that looks like this
Id FirstName
5 Adam
6 Bob
8 Bob
5 Carl
5 Dewie
8 Ernest
When two rows have the same Id, I'd like to keep only one of them. On this example, I would obtain
Id FirstName
5 Adam
6 Bob
8 Bob
Is there concise command to that? I was thinking of
SELECT * FROM Persons HAVING(COUNT(Id)=1)
or
SELECT DISTINCT(Id), FirstName FROM Persons
but my syntax isn't correct.
Hope you are looking for this::
SELECT * from Persons GROUP BY Id
SELECT Id, MIN(FirstName)
FROM Persons
GROUP By Id
Your DISTINCT query will also work, you just need to add GROUP BY id
SELECT DISTINCT(Id), FirstName
FROM Persons
GROUP BY id;
Demo