2015-03-13 39 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。