简体   繁体   English

Mysql 在一行中从同一表中获取不同的值

[英]Mysql get different values from same table in one row

Please, correct the title if it is not clear, unfortunately my English in not perfect, and I'm not sure how to explain my problem in one sentence.如果不清楚,请更正标题,不幸的是我的英语不完美,我不确定如何用一句话解释我的问题。

I have a table of songs and one of artists, this is the songs table:我有一张歌曲表和一位艺术家,这是歌曲表:

|-----------------------------|
| id  | title | artist | date |
|-----------------------------|
| 1   | song1 | 1      | unix |
|-----------------------------|
| 2   | song2 | 1      | unix |
|-----------------------------|
| 3   | song3 | 2      | unix |
|-----------------------------|
| 4   | song4 | 3      | unix |
|-----------------------------|

And this is the artists table这是艺术家表

|----------------------|
| id  | name    | date |
|----------------------|
| 1   | artist1 | unix |
|----------------------|
| 2   | artist2 | unix |
|----------------------|
| 3   | artist3 | unix |
|----------------------|

I would like to get a list of 10 random songs and other 3 song titles.我想获得 10 首随机歌曲和其他 3 首歌曲名称的列表。 I want to make a quiz where users listen to a song and must guess the title, so I need 4 possible answers, the correct and 3 worng titles.我想做一个小测验,用户听一首歌必须猜歌名,所以我需要 4 个可能的答案,正确的和 3 个错误的标题。 So the resoult must be this:所以结果一定是这样的:

|-------------------------------------------------------------------------|
| id  | title | artist_id | artist_name | date | wrong1 | wrong2 | wrong3 |
|-------------------------------------------------------------------------|
| 1   | song1 | 1         | artist1     | unix | song2  | song3  | song4  |
|-------------------------------------------------------------------------|

How can I reach this?我怎样才能做到这一点? Thank you very much, and please let me know if you need other info.非常感谢,如果您需要其他信息,请告诉我。

I would suggest that you use two querys.我建议您使用两个查询。 One should return the correct song.应该返回正确的歌曲。

|--------------------------------------------------|
| id  | title | artist_id | artist_name | date |   |
|--------------------------------------------------|
| 1   | song1 | 1         | artist1     | unix |   |
|--------------------------------------------------|

Another could return three random songs另一个可以返回三首随机歌曲

SELECT * FROM table_name
ORDER BY RAND()
LIMIT 3;

|--------------------------------------------------|
| id  | title | artist_id | artist_name | date |   |
|--------------------------------------------------|
| 2   | song2 | 2         | artist2     | unix |   |
| 3   | song3 | 3         | artist3     | unix |   |
| 4   | song4 | 4         | artist4     | unix |   |
|--------------------------------------------------|

Then you could put the four records together in your code.然后您可以将这四个记录放在您的代码中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MySQL-将具有不同行值的行列值从一个表复制到另一张表(2次) - Mysql - Copy row colum values from one table to another table (2 times) with different row values Mysql:如何用同一表中另一行的值填充一行的空值? - Mysql: How do I fill in null values in one row with values from another row in the same table? 如何从同一mysql表的另一行中的值中减去一行中的值 - How can I subtract the values in one row from the values in another row in the same mysql table 变量应该从mysql表中随机获取值,并且同时属于同一行 - variables should get values randomly from mysql table and at the same time belong to same row MySQL:在同一张表中使用不同标准的当前/未来行值进行计算? - MySQL: Calculations with Current/Future Row Values with different Criteria in same table? MYSQL-选择一个不同的值,并从同一表中选择一个相同的值 - MYSQL - Selecting one different value, and one the same from same table 使用另一张表上的一行中的值更新整行(cake / mysql) - Updating an entire row with values from a row on a different table (cake/mysql) MySQL:使用另一个表中的值在一个查询中多次更新同一行 - MySQL: Update same row multiple times in one query with values from another table 如何在PHPMyAdmin MySQL的同一表中将值从一行复制到另一行 - How to copy values from one row to another in the same table in PHPMyAdmin MySQL mySQL:合并同一表中的列,将值保留在同一行中 - mySQL: Merging columns from the same table keeping values in the same row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM