【问题标题】:How to modify mysql alias for MYSQL EXISTS to 0 and 1 as for records in joined mysql table?如何将 MYSQL EXISTS 的 mysql 别名修改为 0 和 1,就像连接的 mysql 表中的记录一样?
【发布时间】:2020-12-04 12:43:28
【问题描述】:

我有 两个 mysql 表,一个是 products,另一个是 featured_product。

产品表包含我的产品的所有详细信息,包括product_id 作为主键。 在我的特色产品中,我只存储 product_id,它只存储了我的特色产品的 product_id。

我想从 products 表中获取所有记录并将它们加入到 features_product 表中。

我的问题是,当我加入两个表时,我收到了 product_id 用于我精选的行和其他行返回 NULL。 有什么方法可以让我使用 alias 如feature_status 并检索1 用于特色行 和0 用于非特色行? 非常感谢

**products**         **featured_product**
product_id           product_id 
product_name
product_price

product_id    product_name     product_price  feature_status
1              kids Toy          10                NULL
2              Red Pen            5                2

我需要代替这个

product_id    product_name     product_price  feature_status
    1              kids Toy          10                0
    2              Red Pen            5                1

【问题讨论】:

  • 请提供来自两个表的示例数据和所需的输出。
  • @KenWhite 对我朋友的错误表示抱歉。我编辑了它
  • 看起来您需要使用CASE WHEN feature_status IS NULL THEN 0 ELSE feature_status AS feature_status.. 它总是有助于提供示例数据和预期输出以及您自己解决问题的努力(即您的 SQL 语句)。

标签: mysql join


【解决方案1】:

只需选择featured_product.product_id IS NOT NULL AS feature_status

【讨论】:

    猜你喜欢
    • 2022-12-05
    • 1970-01-01
    • 1970-01-01
    • 2023-01-15
    • 1970-01-01
    • 1970-01-01
    • 2014-07-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多