【问题标题】:How to select a mysql column values which contains Y first and N second如何选择包含 Y 第一和 N 第二的 mysql 列值
【发布时间】:2018-04-01 17:24:49
【问题描述】:

我想在 mysql 中查询以选择包含 Y 和 N 的列值。 下面是我的桌子

如果我使用这个查询

"SELECT * from hotel where standard='Y' OR standard='N' group by hotel_code";

这个查询是基于插入ID工作的,但我的要求不是这样,首先它应该先选择'Y'然后只有'N'应该出现。

[![在此处输入图片描述][3]][3]

我想选择特定的这些列值
2 ---- 123 ------是
4 -----324 ------是
6 -----456 ------N 或 5 ------456 -- N 从出现 N 开始的任何行
7 -----987 ------是

提前致谢!!!

【问题讨论】:

  • 那你为什么不按标准订购呢?
  • 我想你的问题和这个有点相似吧? stackoverflow.com/questions/8133531/…
  • 我试过了,但是没用。
  • 如果对于特定的 hotel_code 有一行带有 Y 而另一行带有 N 怎么办?

标签: mysql


【解决方案1】:

我之前的回答有一个问题,表明在 MySql 中执行查询时出现与 only_full_group_by 相关的错误。但是,我自己创建了一个本地数据库,然后提出了您需要的正确 sql。这里是。

SELECT min(origin), hotel_code, max(standard) as std from hotel 
    where standard='Y' OR standard='N' 
    group by hotel_code 
    order by std desc;

在执行 sql 之后,这是我得到的结果。

1   123     Y
3   324     Y
7   987     Y
5   456     N

我正在共享创建表和插入语句,以便任何人都可以自己检查查询是否正常。

create table hotel (
    origin integer auto_increment primary key,
    hotel_code integer not null, 
    standard varchar(1) not null
);

INSERT INTO `hotel` (`origin`, `hotel_code`, `standard`)
VALUES
    (1, 123, 'Y'),
    (2, 123, 'N'),
    (3, 324, 'N'),
    (4, 324, 'Y'),
    (5, 456, 'N'),
    (6, 456, 'N'),
    (7, 987, 'N'),
    (8, 987, 'Y');

希望对您有所帮助!

【讨论】:

  • 将您的查询简化为:SELECT min(origin), hotel_code, max(standart) as std from hotel group by hotel_code order by std desc
  • 太棒了!非常感激。 :)
  • 很高兴知道我可以提供帮助。 @AndrewShmig 也帮助我解决了您的问题。 :)
  • @Reaz Murshed 抱歉再次询问,我检查了您的查询,但它不起作用。我想特别选择下面的 origin 和 hotel_code only (1, 123, 'Y'), (5, 456, 'N'), (4, 324, 'Y'), (8, 987, 'Y') 你的查询选择“Y”,但“原点”正在改变,请检查并帮助我解决。在您的结果中,原点不匹配。提前谢谢!!!
【解决方案2】:

这里有你需要的:

SELECT origin, hotel_code, CASE COUNT(DISTINCT standart)
  WHEN 1 AND standart = "N" THEN "N"
  WHEN 1 AND standart = "Y" THEN "Y"
  WHEN 2 THEN "Y"
END as standart
FROM hotel
GROUP BY hotel_code ORDER BY standart DESC

结果:

origin  hotel_code  standart
1   123     Y
3   324     Y
9   888     Y
7   987     Y
6   456     N

SQLFiddle:http://sqlfiddle.com/#!9/17bd53/3/0

【讨论】:

  • 这不起作用。请修复您的 sql,因为这会在我这边引发以下错误。 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'hotel.origin' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
  • @ReazMurshed,您可以打开 SQLFiddle 并检查它。工作正常。
  • 你的结果全错了。请再次仔细阅读问题。 Mathi 要求进行排序,以便“Y”值的列排在第一位。如问题所述 - 此查询基于插入 ID 工作,但我的要求不是这样,首先它应该先选择“Y”,然后才应该选择“N”。
  • @ReazMurshed,是的,忘记添加 ORDER BY 子句。谢谢。
【解决方案3】:

有很多方法可以解决你的算法,但是,由于简单,我选择了这个:

SELECT h2.origin, h2.hotel_code, h2.standard
FROM (SELECT * FROM > 酒店 WHERE 标准 = 'Y') h1
加入 h1.hotel_code = h2.hotel_code 上的酒店 h2
ORDER BY h2.hotel_code, h2.standard;

Click here to view it working

尽情享受吧!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-06-10
    • 2021-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-01
    • 1970-01-01
    • 2011-03-17
    相关资源
    最近更新 更多