SQL 如何在SQL中选择具有偶数ID号的列?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39446348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 04:52:14  来源:igfitidea点击:

How to select columns with an even ID number in SQL?

sqloracle

提问by Kurt Peek

I'm trying to solve the Hackerrank problem Weather Observation Station 3which is stated below:

我正在尝试解决 Hackerrank 问题Weather Observation Station 3,如下所述:

enter image description here

在此处输入图片说明

I came up with the following solution:

我想出了以下解决方案:

SELECT CITY
FROM STATION
WHERE MOD(ID, 2) = 0

but when I try to run it I get the compiler message "Wrong answer". I don't see what's wrong with this query?

但是当我尝试运行它时,我收到编译器消息“错误答案”。我不明白这个查询有什么问题?

回答by Ted

SELECT DISTINCT CITY
FROM STATION
WHERE MOD(ID, 2) = 0

回答by Vishal Prajapati

You can try this query:

你可以试试这个查询:

SELECT DISTINCT CITY FROM STATION WHERE (ID % 2) = 0 

回答by Rajib Kumer Ghosh

For DB2:- SELECT DISTINCT CITY FROM STATION

对于 DB2:- SELECT DISTINCT CITY FROM STATION

WHERE MOD(ID, 2) = 0;

哪里 MOD(ID, 2) = 0;

For Oracle:- SELECT DISTINCT CITY FROM STATION

对于 Oracle:- 从车站中选择不同的城市

WHERE MOD(ID, 2) = 0;

哪里 MOD(ID, 2) = 0;

For MySQL:- SELECT DISTINCT CITY FROM STATION WHERE MOD(ID, 2) = 0; -- Or -- SELECT DISTINCT CITY FROM STATION

对于 MySQL:- SELECT DISTINCT CITY FROM STATION WHERE MOD(ID, 2) = 0; -- 或 -- 从车站选择不同城市

WHERE ID%2=0;

其中 ID%2=0;

For MSSQL:- SELECT DISTINCT CITY FROM STATION

对于 MSSQL:- SELECT DISTINCT CITY FROM STATION

WHERE ID%2=0;

其中 ID%2=0;

回答by Claire Hu

SELECT distinct CITY FROM STATION
WHERE MOD(ID,2)=0;

回答by Piyush Upadhyay

MYSQL:

MYSQL:

SELECT DISTINCT CITY FROM STATION WHERE (ID % 2)=0;

ORACLE:

甲骨文:

SELECT DISTINCT CITY FROM STATION WHERE MOD(ID, 2)=0;

回答by Gouru Sai

SELECT UNIQUE CITY FROM STATION WHERE MOD(ID,2)=0;

从车站选择唯一城市,其中 MOD(ID,2)=0;

回答by Hassiba Mouncet

I suggest another solution:

我建议另一种解决方案:

    SELECT DISTINCT CITY
    FROM STATION
    WHERE ID LIKE '%0' OR ID LIKE '%2' OR ID LIKE '%4' OR ID LIKE '%6' OR ID LIKE '%8';

回答by Hassiba Mouncet

SELECT DISTINCT CITY FROM STATION WHERE (ID % 2) = 0;

从车站选择 DISTINCT CITY WHERE (ID % 2) = 0;

Distinct for not allowing Duplicates, (Id % 2) tells SQL to divide the Id by 2 and return the remainder. Since we're looking for only even values we set this to "= 0", as 2%2=0, 4%2=0, etc. If we were looking for odd-numbered Ids, we'd use "= 1" instead, since there's always a remainder of 1 when dividing odd numbers by 2. Hope this helps!

区别于不允许重复,(Id % 2) 告诉 SQL 将 Id 除以 2 并返回余数。由于我们只查找偶数值,因此我们将其设置为“= 0”,如 2%2=0、4%2=0 等。如果我们要查找奇数 ID,我们将使用“= 1” " 相反,因为在将奇数除以 2 时总是有 1 的余数。希望这会有所帮助!

回答by Karim Khayloufi

in MySql, you can do :

在 MySql 中,你可以这样做:

SELECT DISTINCT CITY FROM STATION WHERE (ID % 2)=0 ORDER BY CITY ;

回答by Subarata Talukder

If the question would like "Query a list of CITY names from STATION with even ID numbers only" then the answer would be

如果问题是“仅使用偶数 ID 号从 STATION 查询城市名称列表”,那么答案将是

select CITY from station where ID % 2 = 0;

But due the further criteria like "But must exclude duplicates from your answer." your answer should be

但是由于诸如“但必须从您的答案中排除重复项”之类的进一步标准。你的答案应该是

select DISTINCT CITY from station where ID % 2 = 0;