在限制范围内选择NULL值

时间:2019-04-24 13:06:47

标签: mysql sql

我想在限制100之内选择NULL值。左联接应使输出限制为100,并且在其中我要计算所有NULL值。我的subquery似乎无效。有什么想法我做错了吗?预先感谢!

我用SUM()尝试了一些操作,但也无法正常工作

SELECT SUM(echonest.id IS NULL) FROM (SELECT *
FROM tracks, echonest
left JOIN echonest ON tracks.id = echonest.id 
LIMIT 100)

SELECT *
FROM tracks AS t
LEFT JOIN echonest AS e ON t.id = e.id
LIMIT 100 IN 
(SELECT COUNT(e.id)
FROM e
WHERE e.id IS NULL)

预期输出应计算NULL内的所有LIMIT 100值。

2 个答案:

答案 0 :(得分:1)

您可以选择前100行并按如下方式对它们进行计数:-

select count(*) from (
    SELECT t.id
    FROM tracks AS t
    LEFT JOIN echonest AS e ON t.id = e.id
    where e.id is null
    limit 100
) a

答案 1 :(得分:1)

我想这就是你想要的

SELECT COUNT(*) AS tot_nuls
FROM 
(
    SELECT E.id AS eid 
    FROM tracks T
    LEFT JOIN echonest E ON T.id = E.id 
    LIMIT 100
) T
WHERE eid IS NULL

但是,正如注释中多次指定的那样,您应该注意,没有LIMIT子句的ORDER BY子句将返回不确定的(又是随机的)结果。