如何从MySQL语句中获取所有数据?

时间:2013-03-03 21:33:48

标签: mysql join

我的一些数据未在本声明中公布。特别是retaurant_id = 0的那些。是因为,我的entry_id无法与之比较吗?

的MySQL

"SELECT em.*, t.*, cd.*, ct.*, emd.*
FROM exp_members as em
Inner JOIN transactions as t on (em.member_id = t.cardid-10000000)
Inner JOIN exp_channel_titles as ct on (t.restaurant_id = ct.entry_id)
Inner JOIN exp_channel_data as cd on (ct.entry_id = cd.entry_id)
inner join exp_member_data as emd on em.member_id = emd.member_id
WHERE em.member_id = '".$_GET['cardid']."'"

当前输出

Rest_navn   City        Postcode
San Remo    Roskilde    4000    

所需的输出

TransID    Rest_navn    City          Postcode
4214       San Remo     Roskilde      4000 
4212       0            0             0
4211       0            0             0

你可以看到有更多的数据需要出来;但它不会出来。

我已经尝试更改where语句,但没有任何反应。

我的部分数据未在本声明中公布。特别是那些retaurant_id = 0

这是因为t.restaurant_id = null在这一行吗?

Inner JOIN exp_channel_titles as ct on (t.restaurant_id = ct.entry_id)

在我的entry_id

中无法与之比较

2 个答案:

答案 0 :(得分:1)

听起来你需要使用LEFT JOIN - 它不会返回0,但会返回NULL - 如果需要,你可以使用IFNULLCOALESCE返回0:

"SELECT em.*, t.*, cd.*, ct.*, emd.*
FROM exp_members as em
   LEFT JOIN transactions as t on (em.member_id = t.cardid-10000000)
   LEFT JOIN exp_channel_titles as ct on (t.restaurant_id = ct.entry_id)
   LEFT JOIN exp_channel_data as cd on (ct.entry_id = cd.entry_id)
   LEFT join exp_member_data as emd on em.member_id = emd.member_id
WHERE em.member_id = '".$_GET['cardid']."'"

要将0作为值而不是NULL,这样的事情应该会有所帮助(不确定您的表结构)

SELECT COALESCE(Rest_navn,'0')

答案 1 :(得分:1)

我认为你想要外部联接,从事务表开始:

SELECT em.*, t.*, cd.*, ct.*, emd.*
FROM transactions as t
     left outer JOIN exp_members as em on (em.member_id = t.cardid-10000000)
     left outer JOIN exp_channel_titles as ct on (t.restaurant_id = ct.entry_id)
     left outer JOIN exp_channel_data as cd on (ct.entry_id = cd.entry_id)
     left outer join exp_member_data as emd on em.member_id = emd.member_id
WHERE em.member_id = '".$_GET['cardid']."'"

transactionsexp_members之间的第一个连接可以写为内连接,因为where子句将消除外连接带来的额外记录。但是,我认为保持所有连接相同并从您想要所有记录的表开始是最安全的方法。