在Mysql中,当从不同的表中选择数据时的条件

时间:2021-11-07 14:42:56

I have this code and it was working perfectly, but I have added two new columns in my orders table in my database:

我有这个代码,它运行得很好,但是我在数据库的orders表中增加了两列:

  • guest_user ('Y'/'N')

    guest_user(Y / N)

  • guest_phone

    guest_phone

What I want to do is if guest_user is 'y' then I don't have users.email so replace it with orders.guest_phone.

我想做的是如果guest_user是'y'那么我就没有用户。用order .guest_phone替换它。

How to do this. Can anyone fix it please....

如何做到这一点。谁能修复它请....

 SELECT DISTINCT
    orders.id AS 'Order Id',
    users.email AS 'Email',
    orders.price AS 'Amount',
    orders.city_sj AS 'City',
    orders.order_from AS 'Web/App',
    orders.payment_status AS 'COD/PWC',
    orders. AT AS 'Date and Time'

FROM users, orders, order_history    

WHERE users.id = order_history.uid
  AND orders.id = order_history.oid
  AND orders. REAL = 1
  AND orders.price > 1
  AND orders. STATUS = 1
ORDER BY orders.id;

Running the above code i am getting the below type of Output:

运行上述代码,我将得到以下类型的输出:

enter image description here

在这里输入图像描述

Thanks

谢谢

1 个解决方案

#1


4  

Something like this should do the job you want:

像这样的事情应该做你想做的工作:

SELECT DISTINCT orders.id as 'Order Id',  
       IF(orders.guest_user = 'Y', orders.guest_phone, users.email) as 'Phone/Email',  
       orders.price as 'Amount', orders.city_sj as 'City',
       orders.order_from as 'Web/App', 
       orders.payment_status as 'COD/PWC', 
       orders.at as 'Date and Time'
FROM users
JOIN order_history ON users.id = order_history.uid  
JOIN orders ON orders.id= order_history.oid
WHERE orders.real=1 AND orders.price>1 AND orders.status=1
ORDER BY orders.id;

You should also use explicit join syntax instead of old style implicit style.

您还应该使用显式连接语法,而不是老式的隐式样式。

#1


4  

Something like this should do the job you want:

像这样的事情应该做你想做的工作:

SELECT DISTINCT orders.id as 'Order Id',  
       IF(orders.guest_user = 'Y', orders.guest_phone, users.email) as 'Phone/Email',  
       orders.price as 'Amount', orders.city_sj as 'City',
       orders.order_from as 'Web/App', 
       orders.payment_status as 'COD/PWC', 
       orders.at as 'Date and Time'
FROM users
JOIN order_history ON users.id = order_history.uid  
JOIN orders ON orders.id= order_history.oid
WHERE orders.real=1 AND orders.price>1 AND orders.status=1
ORDER BY orders.id;

You should also use explicit join syntax instead of old style implicit style.

您还应该使用显式连接语法,而不是老式的隐式样式。