dtcmadj31951 2014-12-10 10:02
浏览 9
已采纳

从三个表中选择

I have three tables where table_2 is the middle(connected) between table_1 and table_3

tables

table_id
...
...

table_rest

rest_id
table_id
...

rest

rest_id
...
...

And the query to select I use

SELECT m.table_id, table_name
        FROM tables m 
        JOIN table_rest mr 
        ON m.table_id = mr.table_id 
        WHERE rest_id = '$rest_id'

What I need now is to join in this query another table reserv

id
...
status

To check if status is 0, 1,or 2 to not show me anything if there is no status this mean there is no record to show me. In other words this is resserved system where I show on screen few tables. If status is 0,1,2 thats mean the table is taken. If nothing is found for status this mean that there is no record for table and can be shown to user.

EDIT: Sample scenario

tables

table_id
   1
   2
   3
   4
   5

rest

rest_id
   1
   2

table_rest

table_id | rest_id
   1         2
   2         2
   3         2
   4         2
   5         2

So the query that is above will generate 5 tables for rest_id=2 and none for rest_id=1 So now I have another table

reserv

id | status
 1     0
 2     1
 3     2

So in this table reserv currently are saved 3 tables. The idea is to show me other two whit id=4 and id=5 because they are not in table reserv and don't have any status.

Hope is a little bit more clear now.

  • 写回答

2条回答 默认 最新

  • doutuan8887 2014-12-10 10:30
    关注

    You have to point from table reserv to which table is beign booked, let's call it reserv.table_id

    SELECT m.table_id, table_name
            FROM tables m 
            JOIN table_rest mr 
            ON m.table_id = mr.table_id 
            left join reserv
            on reserv.table_id = m.id
            WHERE rest_id = '$rest_id'
            and reserv.status is null   (*note)
    

    *note use 'is' or 'is not' depending of your needs, as far as I read, first seems that you want !=, later that what you want is =

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?