LIMIT在我的查询中不起作用

时间:2016-07-26 06:32:37

标签: mysql

我有以下MySQL查询:

SELECT 
( SELECT count( city) as no_off_total_cityies FROM station ) 
- ( SELECT count(DISTINCT city) as no_off_total_cityies FROM station ) AS sub_value
 FROM station 

工作正常,但当我在查询中添加LIMIT 1时,它无法正常工作

SELECT 
( SELECT count( city) as no_off_total_cityies FROM station ) 
- ( SELECT count(DISTINCT city) as no_off_total_cityies FROM station ) AS sub_value
 FROM station LIMIT 1

4 个答案:

答案 0 :(得分:6)

尝试此查询: -

SELECT (count(city) - count(DISTINCT city)) sub_value from station 

答案 1 :(得分:2)

尝试使用OFFSET

FROM station LIMIT 1 OFFSET 0

此外,您似乎正在尝试查找城市总数与城市不同之间的行数之间的差异,因此您可以尝试将结果设为:

SELECT count( city) - count(DISTINCT city) as no_off_total_cityies FROM station 

在这种情况下,您不需要使用LIMIT,因为结果将是单个值。(非常类似于Rakesh在答案中发布的内容。

答案 2 :(得分:1)

尝试这样:

SELECT sub_value from
((SELECT count( city) as no_off_total_cityies FROM station) - (SELECT count(DISTINCT city) as no_off_total_cityies FROM station)) AS sub_value limit 1

否则: 你可以像这样使用左连接:

select count1-count2 AS RESULT from
(select count(encode) as count1 from tbl_ranking) AS A
LEFT JOIN
(select count(EnName) AS count2 from tbl_ranking) as B 

ON A.ID = B.ID LIMIT 1

希望它有所帮助。

答案 3 :(得分:1)

在这里,您不需要在使用同一个表的子查询的位置使用 from station

我有以下MySQL查询:

选择 (SELECT count(city)as no_off_total_cityies FROM station) - (SELECT count(DISTINCT city)as no_off_total_cityies FROM station)AS sub_value  从车站

工作正常,但当我在查询中添加LIMIT 1时它无法正常工作

SELECT
( SELECT count( city) as no_off_total_cityies FROM station ) 
- ( SELECT count(DISTINCT city) as no_off_total_cityies FROM station ) AS sub_value;

试试这个。希望这有帮助。