数据sql语句问题,关于多表查询的

两张表product和goods,product有的,goods不一定有, 但是又要把没有关联的goods表数据和有关联的表数据查询出来,
这是我写的 查询不行啊
select gs.id id,gs.store_id storeId,gs.pro_id proId,pd.name name,pds.bar_code barCode from goods gs
RIGHT JOIN product pd on pd.id = gs.pro_id
LEFT JOIN product_spec pds on pds.pro_id = pd.id

你是想把两个表的数据一起查出来吗?

select p.*,g.*,ps.* from product p left join goods g on p.id=g.pro_id left join product_spec ps on p.id=ps.pro_id
你的看起来也没问题

我有两张表 一张是goods表
id store_id(门店id) pro_id(产品id) price(价格) ...

1 1 22 4.5

还有一张是product表
id name(产品名称) ..
22 花生
23 瓜皮

有下面的情况, product表有数据, 而goods表中没有数据, 另一种情况是product有数据,而goods表中也有对应的数据

现在我想输入goods表中的store_id=1作为检索条件,把store_id存在的和不存在的全部展示出来,我想实现的效果如下

id name store_id price
1 花生 1 4.5
null 瓜皮 null null

我的sql显示不了
select gs.id id,gs.store_id storeId,gs.gs.pro_id proId,pd.name name from goods gs
RIGHT JOIN product pd on pd.id = gs.pro_id
LEFT JOIN product_spec pds on pds.pro_id = pd.id
where 1=1
and gs.store_id = null or gs.store_id = 1

谢谢谢谢谢谢谢谢谢谢

内链接查询
select gs.id id,gs.store_id storeId,gs.gs.pro_id proId,pd.name name from goods gs,product pd,product_spec pds where pd.id = gs.pro_id and pds.pro_id = pd.id and (gs.store_id = null or gs.store_id = 1)

额,错了。是外链接查询

为啥不用full join

mysql> select * from goods;
+----+-----+-----+-------+
| id | sid | pid | price |
+----+-----+-----+-------+
| 1 | 1 | 22 | 4.5 |
+----+-----+-----+-------+
1 row in set (0.00 sec)

mysql> select * from product;
+----+--------+
| id | name |
+----+--------+
| 22 | 花生 |
| 23 | 瓜皮 |
+----+--------+
2 rows in set (0.00 sec)

mysql> select g.id id ,g.sid sid,g.pid pid,p.name name,g.price price from goods g right join product p on p.id=g.pid where g.sid = 1 or g.sid is null;
+------+------+------+--------+-------+
| id | sid | pid | name | price |
+------+------+------+--------+-------+
| 1 | 1 | 22 | 花生 | 4.5 |
| NULL | NULL | NULL | 瓜皮 | NULL |
+------+------+------+--------+-------+
2 rows in set (0.00 sec)

mysql>

select gs.id , gs.store_id , gs.pro_id , pd.name , pds.bar_code

from goods as gd , product as pro
where gd.pro_id=pro._id

两个表的关系是这样的,goods表 的属性(pro_id )是product表 属性(id)的外键

(select 后面接的属性,他们之间是有逗号分隔符的)
稍微修改了一些,结果应该是一样的,不对勿喷

select gd_pts.id,....(这里楼主自己写吧,就是最终表里面的数据)  from (select gs.id id,gs.store_id ,gs.pro_id proId,pd.name name  from goods gs RIGHT JOIN product pd on 
pd.id = gs.pro_id LEFT JOIN product_spec pds on pds.pro_id = pd.id)  gd_pts left join product LEFT JOIN product_spec pds on pds.pro_id = pd.id

括号里面的select语句是查询的中间表,可能写的不对,只是给你提供个思路,是可以这样分层查询的,把大问题分解成小问题,然后小问题里面只要没毛病,大问题也就OK了
,就算出问题了,从小的问题里面再找问题也就简单了...
嗯,还有,这里只是提供个建议,思路是正确的,具体能不能达到要求我也没测试,所以,见谅哈..