的問題是內連接的過濾掉哪些是不相關的表,但在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