Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何基于相关表获取数据?_Mysql_Sql - Fatal编程技术网

Mysql 如何基于相关表获取数据?

Mysql 如何基于相关表获取数据?,mysql,sql,Mysql,Sql,我试图构建2个应该很简单的查询 需要做的第一件事是统计listingcats表中与给定类别ID对应的listingcats表中的列表数量 第二部分是获取listingcats表中与listingcats表中给定类别id相关的行中的所有数据* 我已经尝试了许多连接,但出于某种原因,没有一个连接能够正常工作。有人能根据上面给出的示例表提供帮助吗。本例中的“给定”类别ID为“1”。对于第一个查询,您可以使用简单的联接并返回计数 Listings table +------------+--------

我试图构建2个应该很简单的查询

需要做的第一件事是统计listingcats表中与给定类别ID对应的listingcats表中的列表数量

第二部分是获取listingcats表中与listingcats表中给定类别id相关的行中的所有数据*


我已经尝试了许多连接,但出于某种原因,没有一个连接能够正常工作。有人能根据上面给出的示例表提供帮助吗。本例中的“给定”类别ID为“1”。

对于第一个查询,您可以使用简单的联接并返回计数

Listings table
+------------+---------+
| name       | id      |
+------------+---------+
| Example 1  |  1      |
| Example 2  |  2      |
| Example 3  |  3      |
| Example 4  |  4      |
| Example 5  |  5      |
| Example 6  |  6      |
+------------+---------+

Categories table
+------------+---------+
| name       | id      |
+------------+---------+
| Catname 1  |  1      |
| Catname 2  |  2      |
| Catname 3  |  3      |
+------------+---------+

ListingCats table
+--------+---------+
| cat_id | list_id |
+--------+---------+
| 1      | 1       |
| 1      | 2       |
| 1      | 3       |
| 2      | 1       |
| 3      | 1       |
| 3      | 3       |
| 2      | 2       |
| 1      | 5       |
| 2      | 6       |
+--------+---------+
这将返回listings表中的所有行,以便listings id在listingcats表中具有相应的cat_id,但仅限于cat_id为1的行。然后,count aggregate函数返回行数

对于第二个,您可以只使用上面相同的子查询,但不使用聚合函数,并选择所有值

SELECT COUNT(Name)
FROM Listings l
JOIN ListingCats lc ON l.id = lc.cat_id
WHERE lc.cat_id = 1
试试这些,请让我知道它们是否有效,我将与您一起努力解决它们

编辑

回顾问题后,如果您获得了一个特定的cat_id,您甚至不需要使用联接,您只需在listings表中查询一个具有该id的id。如果给定的id是一个:

SELECT * FROM Listings l
JOIN ListingCats lc ON l.id = lc.cat_id
WHERE lc.cat_id = 1
再一次,第二个更广泛:

SELECT COUNT(Name)
FROM Listings l
WHERE l.id = 1

你能添加到目前为止所做的查询吗?从你发布的示例数据来看,你希望得到什么样的输出?这些都不是很难的连接,发布你的尝试,这将是一件“愚蠢”的事情,只是第二个查询看起来甚至需要连接。我想您的第一个查询就是从listingcats group中选择cat_id,count*,cat_idBrian:您是对的。实际上我只需要稍微修改一下,但它是有效的。更改在l.id=lc.list____________________________________________!
SELECT * FROM Listings l WHERE l.id = 1
CREATE TABLE  `listings` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `name` varchar(10) NOT NULL default '0',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

CREATE TABLE  `categories` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `name` varchar(10) NOT NULL default '0',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

CREATE TABLE  `listings_cats` (
  `cat_id` int(10) unsigned NOT NULL,
  `list_id` int(10) unsigned NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;


SELECT c.id, c.name, COUNT(lc.list_id) as the_count 
FROM categories c 
JOIN listings_cats lc ON (lc.cat_id = c.id) 
GROUP BY c.id;

SELECT l.id, l.name, c.name AS category_name 
FROM listings l JOIN listings_cats lc ON (lc.list_id = l.id) 
JOIN categories c ON (lc.cat_id = c.id);