从多个表中获取数据会产生较少的记录

时间:2015-03-13 06:46:29

标签: php mysql

我有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

2 个答案:

答案 0 :(得分: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 |
+------+------+-------------+---------+

以上示例与您所拥有的相似。

答案 1 :(得分: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
  1. 查看结果并查看空位,然后您会发现jos_sobi2_item中哪些记录在其他表中没有连接。
  2. 或者如果您想使用LEFT JOIN: 1.运行查询:

    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. 分析重复记录的计数,您可以将条件设​​置为count>每次计数都是1。