如何将多个表组合成一个sql语句

时间:2013-02-25 13:36:28

标签: php sql join

我有4个表,我想加入一个sql语句。

这些是我的表格:

  1. exp_members :card_id(pk)
  2. 交易:card_id(pk),restaurant_id(pk)
  3. exp_channel_data :restaurant_id(pk)
  4. exp_channel_titles :restaurant_id(pk)
  5. 我已经尝试过这样的声明,但这不起作用

        SELECT cm.*, t.*, cd.*, ct.*
    FROM exp_members as cm
    Inner JOIN transactions as t on (cm.card_id = t.restaurant_id)
    Inner JOIN exp_channel_titles as ct on (ct.restaurant_id = t.restaurant_id)
    Inner JOIN exp_channel_data as cd on (cd.restaurant_id = ct.restaurant_id)
    order by t.created DESC limit 50
    

1 个答案:

答案 0 :(得分:0)

如果你这样做:

SELECT t.*. cd.*, ct.*
FROM transactions as t
LEFT JOIN exp_channel_titles as ct on (t.restaurant_id = ct.restaurant_id)
LEFT JOIN exp_channel_data as cd on (cd.entry_id = ct.entry_id)
WHERE t.cardid > 0 
and t.restaurant_id > 0
and ct.status= 'open'
order by t.created DESC limit 50

它给你一些东西??? ...我很抱歉但很难找到没有你的表格结构的简单解决方案:)

Saludos。