简体   繁体   中英

can't get all data from table in select query

this is my table in phpmyadmin name documents. In html form, dropdown of subject name in not compulsory to select. So it can be blank and get 0 that shown in table. But when I run select query to get all documents list, can't get data of sub_id which value is 0. I want all data from documents table. Plz solve this.

$list=mysqli_query($con,"select documents.*,courses.cour_name,subjects.sub_name from documents join courses on documents.cour_id=courses.cour_id join subjects on documents.sub_id=subjects.sub_id") or die(mysqli_error($con));


doc_id=primary key,
cour_id=index key,
sub_id=index kry


doc_id  cour_id  sub_id  doc_name   
  1       1         0                               
  6       1         2     MB-C250D-AvantGarde-2012.JPG  
  7       1         1     adview.sql    
  8       1         2     crane.txt     
  9       1         0     ui.txt    

result after run select query

Document Id     Document Name                  Course    Subject    
6               MB-C250D-AvantGarde-2012.JPG   B.com     Statistics     
7               adview.sql                     B.com     Accounting     
8               crane.txt                      B.com     Statistics     

Use LEFT JOIN instead:

select documents.*,courses.cour_name,subjects.sub_name
from documents
LEFT join courses on documents.cour_id=courses.cour_id 
LEFT join subjects on documents.sub_id=subjects.sub_id

Simple JOIN re-validate existence of data in both tables

$list=mysqli_query($con,"select documents.*,courses.cour_name,subjects.sub_name from documents left join courses on documents.cour_id=courses.cour_id left join subjects on documents.sub_id=subjects.sub_id") or die(mysqli_error($con));

使用左联接,因为可能有一些其他表中不存在的数据。因此联接将丢弃那些不常见的数据。

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