如何只显示那些至少创建了一个产品的卖家

ozxc1zmp  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(215)

我有一个sellers表,其中sellerid列是 sno 我有一张产品表,里面有sellerid sellerid ,我想得到那些没有被阻止的卖家(有一列名为 flag 在sellers表中),并且已经创建了至少一个产品,所以我写了这个(或者复制这个)

SELECT e.*, count(*) AS count 
FROM sellers AS e  
left join products AS r ON e.sno = r.sellerid 
where NOT e.flag='1' 
GROUP BY e.sno

现在我通过以下方式来分析我的逻辑:

if($row["count"] == 1){
continue;
}

它产生了错误的结果

x7yiwoj4

x7yiwoj41#

-- fake table data
CREATE TABLE sellers SELECT 1 sno, 0 flag UNION SELECT 2, 0 UNION SELECT 3, 0;
CREATE TABLE products SELECT 1 sellerid UNION ALL SELECT 1 UNION ALL SELECT 2;
SET sql_mode := '';
-- initial query
SELECT e.*, count(*) AS `count` 
FROM sellers AS e  
left join products AS r ON e.sno = r.sellerid 
where NOT e.flag='1' 
GROUP BY e.sno

snoflagcount102201301号

-- counting joined values, not rows (NULLs are ignored)
SELECT e.*, count(r.sellerid) AS `count` 
FROM sellers AS e  
left join products AS r ON e.sno = r.sellerid 
where NOT e.flag='1' 
GROUP BY e.sno
-- further filtering by count>0 needed

snoflagcount102201300号

-- join only rows which have matched rows in second table
SELECT e.*, count(*) AS `count` 
FROM sellers AS e  
inner join products AS r ON e.sno = r.sellerid 
where NOT e.flag='1' 
GROUP BY e.sno
-- no further filtering needed

snoflagcount102201号
db<>在这里摆弄

相关问题