gpt4 book ai didi

mysql - 在 mysql 查询中使用 if 语句

转载 作者:太空宇宙 更新时间:2023-11-03 11:52:14 25 4
gpt4 key购买 nike

我想在 sql 查询中使用 if 语句:

我想要什么:

if(tractions_delivery.send_date_id !=0 ){

date_send_commodities.id = tractions_delivery.send_date_id
}

我的查询:

        from

tractions_delivery,user_address,province,city,date_send_commodities,users

WHERE

tractions_delivery.tr_id = $tr_id
AND
tractions_delivery.address_id = user_address.id
AND
user_address.province_id = province.id
AND
user_address.city_id = city.id
AND
//not work
(tractions_delivery.send_date_id IS NOT 0 date_send_commodities.id = tractions_delivery.send_date_id)

AND
users.id = user_address.user_id

最佳答案

你可以使用 CASE 语句

SELECT 
*
FROM
tractions_delivery,
user_address,
province,
city,
date_send_commodities,users
WHERE
tractions_delivery.tr_id = $tr_id AND
tractions_delivery.address_id = user_address.id AND
user_address.province_id = province.id AND
user_address.city_id = city.id AND
CASE WHEN tractions_delivery.send_date_id != 0 THEN date_send_commodities.id = tractions_delivery.send_date_id ELSE 1=1 END AND
users.id = user_address.user_id

关于mysql - 在 mysql 查询中使用 if 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34850735/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com