sql - 使用 IF 条件运行 sql 查询时出错
问题描述
运行查询时出现错误
INSERT INTO `received_order`(
`retailer_name`,
`order_id`,
`dboy_username`,
`product_id`,
`quantity`,
`create_date`,
`timestamp`,
`payment_status`,
`delivery_status`,
`device_type`
)
VALUES(
'ankur',
'OD123',
'manish',
'PIDEAB565',
(
SELECT
`product_quantity` IF(product_quantity > 50, 50, 0)
FROM
`master_stock`
WHERE
`product_id` = 'PIDEAB565'
),
'',
'',
'',
'',
''
)
MySQL 说:
1064 - 您的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册,以
master_stock
在第 20 行的 'IF(product_quantity 50, 50, 0) FROM WHERE'附近使用正确的语法
解决方案
inline product_quantity IF(product_quantity > 50, 50, 0)
IF 不能这样使用。您应该改用case
语句。
尝试这个
INSERT INTO `received_order`(
`retailer_name`,
`order_id`,
`dboy_username`,
`product_id`,
`quantity`,
`create_date`,
`timestamp`,
`payment_status`,
`delivery_status`,
`device_type`
)
VALUES(
'ankur',
'OD123',
'manish',
'PIDEAB565',
(
SELECT
case when `product_quantity`>50 then 50 else 0 end
FROM
`master_stock`
WHERE
`product_id` = 'PIDEAB565'
),
'',
'',
'',
'',
''
)
推荐阅读
- java - 在 JFrame 类中使用 main 方法还是只调用 JFrame?
- sql - 如何在 EAV 模型中将字符串作为 int 进行比较
- java - 使用 Array 和 while 循环的询问数字的 Java 索引
- android - 使用编辑文本在片段内的 recyclerview 中搜索
- docker - 创建 Dockercompose 和 Dockerfile 时出现问题。导致“来自守护程序的错误响应”
- python - 如何编辑列表中的值
- php - php中的sql更新查询
- timer - DS3231 RTC 模块上的 SQW 信号卡在 1Hz(需要 1024Hz)
- javascript - 什么是 Try-Catch 真正吸引人
- c# - EF Core 多对多重复条目