将第一行 mysql table 中的一行连接到第二行 table 中的多行

join one row from one mysql table to multiple row in second table

我对 mysql 查询有疑问。故事是这样的:我有 table 来存储有关大学旅行的信息。这个table有行程名称、行程id和activity的属性。 Activity 可以是 0 或 1,具体取决于跳闸是否仍处于活动状态 (1) 或非活动状态 (0)。在第二个 table 中,我有关于申请旅行的学生的信息,这些信息具有以下属性:id、姓名、姓氏和学生申请的旅行的 id。我不知道 mysql 查询只会向我显示已申请仍然有效的旅行 (acitivity=1) 的学生。

例如让我们看看这些 tables:

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