的问题是内连接的过滤掉哪些是不相关的表,但在jos_sobi2_item
记录
您可能想要做left join
东西如
select
a.itemid as ID,
a.title as Name,
c.name as Categories,
d.data_txt as Description,
d.fieldid as FieldID
FROM `jos_sobi2_item` as a
left join `jos_sobi2_cat_items_relations` as b ON b.itemid=a.itemid
left join `jos_sobi2_categories` as c ON c.catid = b.catid
left join `jos_sobi2_fields_data`as d ON a.itemid=d.itemid
下面是这种
create table table1 (t1id int , name varchar(100));
insert into table1 values (1,'aa'),(2,'cc'),(3,'dd'),(4,'ee'),(5,'ff'),(6,'bb'),(7,'gg');
create table table2 (t2id int, description varchar(100));
insert into table2 values (1,'desc1'),(2,'desc2'),(3,'desc3');
create table table3 (t3id int, t1id int , t2id int);
insert into table3 values (1,1,2),(2,3,2),(3,2,2),(4,7,3);
create table table4 (t4id int , t1id int ,fieldid int);
insert into table4 values (1,1,10),(2,3,23),(3,4,34),(4,5,50);
select
t1.t1id,
t1.name,
t2.description,
t4.fieldid
from table1 t1
left join table3 t3 on t3.t1id = t1.t1id
left join table2 t2 on t2.t2id = t3.t2id
left join table4 t4 on t4.t1id = t1.t1id;
+------+------+-------------+---------+
| t1id | name | description | fieldid |
+------+------+-------------+---------+
| 1 | aa | desc2 | 10 |
| 2 | cc | desc2 | NULL |
| 3 | dd | desc2 | 23 |
| 4 | ee | NULL | 34 |
| 5 | ff | NULL | 50 |
| 6 | bb | NULL | NULL |
| 7 | gg | desc3 | NULL |
+------+------+-------------+---------+
的说明,上面的例子类似,你有什么。
是否确定在与'jos_sobi2_item'连接的相关表中有相同的记录量或换句话说'inner join'只会给你满足连接条件的数据。 – 2015-03-13 06:58:58
尝试'LEFT JOIN'。 – 2015-03-13 07:01:25
@AbhikChakraborty jos_sobi2_item有548条记录,所以我认为我的查询需要有548条记录,但由于某种原因它有439条 – jureispro 2015-03-13 07:01:38