以下问题需要什么SQL命令?

时间:2019-03-31 13:52:36

标签: php mysql sql join select

我试图通过使用已登录用户的用户名(链接到与约会相关联的PatientNumber)显示所有约会。只是想知道哪种SQL语句能够执行此操作?

我的表格如下:

约会

APNo    Weekstart   Day APTime  DNo PNo
   1    01-01-2019   M   9:00    1  1

患者

PNo  FIRSTNAME     LASTNAME  ADDRESS       username
 1     Joe            Bloggs  1 The Road    joe123

用户

id    username   password
 1      joe123    *hashedpassword*

更多上下文,这将通过使用会话用户名的php

4 个答案:

答案 0 :(得分:1)

SELECT * FROM Appointment A, Patient P WHERE A.PNo = P.PNo AND P.username = 'theUsernameOfYourUser';

由于A.PNoP.PNo具有相同的列名,因此您甚至可以使用NATURAL JOIN(较短的语法):

SELECT * FROM Appointment NATURAL JOIN Patient WHERE username = 'theUsernameOfYourUser';

答案 1 :(得分:0)

请尝试在php中查询以下内容

$username = $_SESSION['username'];

$sql = "Select u.username, p.FIRSTNAME, a.WeekStart, a.APTime From Patient p,User u,Appointment a
where p.username='$username' AND p.PNo = A.PNo"

答案 2 :(得分:0)

尝试以下语句:

SELECT Appointment.* ,temp.username FROM Appointment,
(SELECT PNo as Pno, username AS username FROM Patient,
User WHERE Patient.username=User.username) AS temp 
WHERE temp.Pno=Appointment.PNo;

答案 3 :(得分:0)

加入表格,并可能在"scripts": { "post-update-cmd": [ "MyApp\\Composer\\ScriptHandler::myScript" ] } 上应用过滤器:

Weekstart