2015-03-13 61 views
0

我有Joomla表(如下圖所示)與其中的一些記錄(記錄是關於公司)。我想實現的是獲取所有記錄並將它們分組到新表(我正在使用PHP)。問題是有548條記錄(公司),我有439條記錄(分組後)。從多個表獲取數據結果與更少的記錄

有人知道問題在哪裏嗎?

這裏是數據庫:

Database

這裏是查詢:

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 
     INNER JOIN `jos_sobi2_cat_items_relations` as b ON b.itemid=a.itemid 
     INNER JOIN `jos_sobi2_categories` as c ON c.catid = b.catid 
     INNER JOIN `jos_sobi2_fields_data`as d ON a.itemid=d.itemid 
+0

是否確定在與'jos_sobi2_item'連接的相關表中有相同的記錄量或換句話說'inner join'只會給你滿足連接條件的數據。 – 2015-03-13 06:58:58

+0

嘗試'LEFT JOIN'。 – 2015-03-13 07:01:25

+0

@AbhikChakraborty jos_sobi2_item有548條記錄,所以我認爲我的查詢需要有548條記錄,但由於某種原因它有439條 – jureispro 2015-03-13 07:01:38

回答

1

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

的說明,上面的例子類似,你有什麼。

+0

感謝您的幫助。我嘗試了左連接,但我得到了更多的1條記錄。 – jureispro 2015-03-13 07:14:38

+0

你確定表'jos_sobi2_item'有'548'記錄嗎?做一個'從jos_sobi2_item'選擇coount(*)並查看有多少條記錄。同時仔細檢查一下,您是否在執行查詢的同一個數據庫中。我用類似於你的數據庫結構的圖解更新了答案。 – 2015-03-13 07:26:16

0

如果你仍然想使用INNER 1.嘗試LEFT JOIN代替INNER JOIN,並提出一個條件WHERE

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 
WHERE b.itemid IS NULL OR c.catid IS NULL OR d.itemid IS NULL 
  • 看看你的結果,看看您有空位,之後您將發現jos_sobi2_item的記錄在其他表中沒有連接。
  • 或者,如果你想使用LEFT JOIN: 1.Run查詢:

    SELECT a.itemid as ID, a.title as Name, c.name as Categories, d.data_txt as Description, d.fieldid as FieldID, 
    COUNT(b.itemid), COUNT(c.catid), COUNT(d.itemid) 
    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 
    GROUP BY a.itemid 
    
  • 分析計數重複的記錄,你可以把這樣一種情況:每次計數> 1。