繁体   English   中英

将一行从一个mysql表连接到第二个表中的多行

[英]join one row from one mysql table to multiple row in second table

我对mysql查询有疑问。 这个故事是这样的:我有一张桌子,上面存储着大学旅行的信息。 该表具有有关旅程名称,旅程ID和活动的属性。 活动可以是0或1,具体取决于跳闸仍处于活动状态(1)还是非活动状态(0)。 在第二张表中,我提供了有关已申请旅行的学生的信息,其属性为:该学生已申请旅行的ID,姓名,姓氏和ID。 我不知道mysql查询不会向我显示仅已申请旅行但仍处于活动状态(acitivity = 1)的学生。

例如,让我们看一下这些表:

TRIPS

id | trip     | activity
---+----------+-----------
1  | Paris    | 0
2  | London   | 1
3  | Belgrade | 0
4  | Prague   | 1

STUDENTS

id | name     | id_trip
---+----------+-----------
1  | Mark     | 3
2  | Ana      | 1
3  | Tom      | 2
4  | Maya     | 3
5  | Rachel   | 4
6  | John     | 2



   RESULT

    id | name     | id_trip | trip    | activity
    ---+----------+---------+---------+---------
    3  | Tom      | 2       | London  | 1
    5  | Rachel   | 4       | Prague  | 1
    6  | John     | 2       | London  | 1

尝试这个:

SELECT s.id, s.name, s.id_trip, t.name, t.activity
FROM students s
JOIN trips t
ON s.id_trip = t.id
WHERE t.activity = 1
select * from students s
join trips t
on  s.id_trip = t.id
where t. activity =1
SELECT 
s.id, 
s.name, 
s.id_trip, 
t.trip, 
t.activity
FROM 
STUDENTS AS s
INNER JOIN TRIPS AS t ON ( t.id = s.id_trip ) 
WHERE
t.id = 1

希望这会起作用。

试试这个可以解决你的问题:

select s.id as id, s.name as name,t.trip as trip, 
t.activity as activity from trips t 
join  students s on 
t.id = s.id_trip 
where t.activity = 1

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM