【问题标题】:How to combine multi tables into one sql statement如何将多表合并为一条sql语句
【发布时间】:2013-02-25 13:36:27
【问题描述】:

我有 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)

我已经尝试过这样的声明,但这不起作用

    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

【问题讨论】:

  • 它不起作用是什么意思?你看到的记录不是预期的结果?还是什么?
  • 您希望他们如何加入? LEFT JOIN 包括查询左侧表中的所有值(例如事务),无论匹配如何。
  • “没有工作”没有描述预期的行为和收到的结果/错误
  • 为什么不起作用?你有什么错误吗?
  • 第一个join是ct,我觉得应该在cm table之前join

标签: php sql join


【解决方案1】:

如果你这样写:

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

它给了你一些东西???.....对不起,如果没有你的表格结构,很难找到一个简单的解决方案:)

感谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多