简体   繁体   中英

how to get data from intersection of 2 SQL tables to php

I want to take data from intersection of 2 SQL tables. I tried this

$sql = "SELECT ID, Name, Hostel FROM Student, HostelName WHERE Student.hostel_ID = Hostel.hostel_ID";

But it Did not work. I also tried this too.

$sql = "SELECT Student.ID, Student.Name, Hostel.HostelName FROM Student, Hostel WHERE Student.hostel_ID = Hostel.hostel_ID";

How can I fix this problem.

你的意思是 :

$sql = "SELECT Student.ID, Student.Name, Hostel.HostelName FROM Student INNER JOIN Hostel ON Student.hostel_ID = Hostel.hostel_ID"; 

Assuming the fact that hostel_ID is the only attribute which is common between the two tables, you can do NATURAL JOIN on common attribute hostel_ID to achieve the desired result,

$sql = "SELECT ID, Name, Hostel FROM Student NATURAL JOIN HostelName";

Here's the reference:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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