在PHP中使用IF条件检索错误的mysql数据

时间:2017-10-04 07:09:39

标签: php mysql

我正在使用下面的mysql查询,

$userlist_sql = "SELECT id,role,to_role,type,points,company_code,total_pairs,invoice_no,invoice_date,division,created_at, 
IF( role = '6', (select name from dealers where dealers.id=loyality_points_history.user_id and dealers.state='{$state_code}'), (select name from sub_dealers where sub_dealers.id=loyality_points_history.user_id and sub_dealers.state='{$state_code}' )) AS from_name,
IF( role = '6', (select customer_id from dealers where dealers.id=loyality_points_history.user_id and dealers.state='{$state_code}'), (select customer_id from sub_dealers where sub_dealers.id=loyality_points_history.user_id and sub_dealers.state='{$state_code}' )) AS from_userID,
IF( role = '6', (select city from dealers where dealers.id=loyality_points_history.user_id and dealers.state='{$state_code}'), (select city from sub_dealers where sub_dealers.id=loyality_points_history.user_id and sub_dealers.state='{$state_code}' )) AS from_city,
FROM loyality_points_history  where from_userID != null";

如果from_userID为空,则不想从loyality_points_history表中获取相应的数据。通过使用上面的查询获取错误“未知列from_userID”。但我想检查from_userID是否为空。

0 个答案:

没有答案