I have a table named User with a list of names in that table. I want to run a single SQL string that returns a list of names without the name of the first record in consecutive order. The omitted name will be the column's header.
I have tried:
$result = mysql_query("SELECT name FROM user AND NOT (name = 'David')");<br>
and<
$result = mysql_query("SELECT name FROM user (LIMIT 1)");
The 1st result should be:
Tim
Jeremy
Mike
David
With the name Roberto
being omitted.
The 2nd result should be:
Roberto
Jeremy
Mike
David
With the name Tim
being omitted.
The 3rd result should be:
Roberto
Tim
Mike
David
With the name Jeremy
being omitted.
And so on..
You can join the table with itself, and use GROUP_CONCAT()
to get all the other names.
SELECT u1.name, GROUP_CONCAT(u2.name) AS others
FROM user AS u1
JOIN user AS u2 ON u2.name != u1.name
others
will be a comma-separated list of all the other names. So it will look like:
name others
Roberto Tim,Jeremy,Mike,David
Tim Roberto,Jeremy,Mike,David
Jeremy Roberto,Tim,Mike,David
Mike Roberto,Tim,Jeremy,David
David Roberto,Tim,Jeremy,Mike
You can then use explode()
in PHP to split it into an array.
If you have lots of names, make sure you set group_concat_max_len
high enough to allow all the names.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.