Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 从同一个表中选择两行_Mysql_Sql - Fatal编程技术网

Mysql 从同一个表中选择两行

Mysql 从同一个表中选择两行,mysql,sql,Mysql,Sql,) 我希望在一组联接的顶部从同一个表中选择两行,但我不确定具体要做什么。 这是我当前的select语句: SELECT m_table.*, t_people.name as boxer_name, t_people.class, t_people.age, t_people.sex, m_time.time, m_time.rounds, t_age.name as age_name, teams.name as team_name FROM m_table INNER JOIN t_peop

) 我希望在一组联接的顶部从同一个表中选择两行,但我不确定具体要做什么。 这是我当前的select语句:

SELECT m_table.*, t_people.name as boxer_name, t_people.class, t_people.age, t_people.sex, m_time.time, m_time.rounds, t_age.name as age_name, teams.name as team_name
FROM m_table
INNER JOIN t_people ON t_people.id = m_table.red_id
INNER JOIN m_time ON (m_time.age = t_people.age AND m_time.sex = t_people.sex OR m_time.age = t_people.age AND m_time.sex = 'u')
INNER JOIN t_age ON t_age.id = t_people.age
INNER JOIN teams ON teams.id = t_people.team_id
我想从Tu people表中提取两个人

编辑 正如您在上面看到的,我使用m_table.red_id提取了一个 我希望使用m_table.blue_id选择另一行 编辑


应该如何实现这一点?

只需添加另一个
JOIN
和另一个别名即可

INNER JOIN t_people people1 ON people1.id = m_table.red_id
INNER JOIN t_people people2 ON people2.id = m_table.blue_id

然后使用此别名从中选择值,例如,
people1.name

是否要选择前两行?可以使用Limit 2Nope我希望从t_people中选择id为m_table.red_id和m_table.blue_id的行选择top 2,*…。查询的其余部分。如果你想坐在第一排。如果你想要随机的或其他的,请告诉我们。将只显示查询列表的前两个结果这似乎是一个不错的方法,但是它给我留下了以下错误:“field list”@Mathias中的未知列“t_people.name”,因为使用别名时没有
t_people
。我尝试在blue_id上只使用一个别名,但结果是没有从blue输出任何内容。我需要在select语句中分配另一个选择吗?@Mathias你必须列出所有要选择的字段,例如,如果你想
name
,那么你必须写
people1.name,people2.name
。是的,这就是我的想法:-)我很快会尝试,然后再回复你。非常感谢