简体   繁体   English

PHP三个SQL选择语句

[英]PHP three SQL select statement

i have one database with three tables and i am trying to get some information for all the tables 我有一个包含三个表的数据库,我正在尝试获取所有表的一些信息

activities -> contains events/ acivities and uses the userIds of another table to identify the row. activities ->包含事件/活动,并使用另一个表的userId标识行。

teams -> contains a listing of teams (including the id stored in activities) and the attributed assigned to each team such as name away colors etc. teams ->包含团队列表(包括存储在活动中的ID)以及分配给每个团队的属性,例如名称更改颜色等。

facilities -> contain list of all the facilities activities can be booked at facilities ->包含可在以下位置预订的所有设施活动的列表

I would like to create a select statement that uses the activities table to get the home and away teams then look up the teams names (not ids which is stored in the activities database) and get the name of the facility . 我想创建一个使用activities表获取主队和客队的select语句,然后查找队名(而不是存储在活动数据库中的id)并获取facility的名称。

i have used the following sql 我已经使用以下sql

$select = "SELECT *, teams.name 
            FROM activities, teams 
            WHERE (SELECT * 
                    FROM teams 
                    WHERE teams.id=activities.teamIdAway) activities.status=(:s) 
                AND activities.dateStart >= (:start) 
                AND activities.dateEnd <= (:end) 
                AND activities.facilityId=(:f)";

You need to do two join with the teams table, one for the home team, another for the away team. 您需要在“ teams表中进行两次连接,一个用于主队,另一个用于客队。

SELECT f.name, ht.name AS hometeam, at.name AS awayteam
FROM facilities AS f
JOIN activities AS a ON a.facilityId = :f
JOIN teams AS at ON at.id = a.teamIdAway
JOIN teams AS ht ON ht.id = a.teamIdHome
WHERE a.dateStart >= :start
AND a.dateEnd <= :end
AND a.status = :s

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

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