我想用一个语句从同一表中选择不同行的不同列。我将如何将其结合在一起?
SELECT `shop_lat_log` FROM `customers` WHERE `phoneNumber`='254719401837'
SELECT `delivery_lat_log` FROM `customers` WHERE `phoneNumber`='25472054919'
您可以将两个当前查询中的每一个用作一个subqueries在一个选择语句中:
SELECT
(SELECT shop_lat_log FROM customers WHERE phoneNumber = '254719401837') AS shop_lat_log,
(SELECT delivery_lat_log FROM customers WHERE phoneNumber = '25472054919') AS delivery_lat_log
FROM dual;
这假设您的两个查询中的每个问题都返回一个值。如果不是,那么也许UNION
更合适:
SELECT
shop_lat_log AS log_value,
'shop_lat_log' AS log_type
FROM customers
WHERE phoneNumber = '254719401837'
UNION ALL
SELECT
delivery_lat_log,
'delivery_lat_log'
FROM customers
WHERE phoneNumber = '25472054919'
您可以使用UNION
:
SELECT `shop_lat_log` FROM `customers` WHERE `phoneNumber`='254719401837'
UNION
SELECT `delivery_lat_log` FROM `customers` WHERE `phoneNumber`='25472054919'
请注意,第二个查询必须具有与第一个查询相同的列数,并且结果将具有 first 查询列名称。
因此,即使您正在选择第二个查询中的delivery_lat_log
列,如果您要获取关联数组,则结果也将在shop_lat_log
列中。
使用SQL case
语句
select case c.phone_number
when '254719401837'
then c.shop_lat_log
when '25472054919'
then c.delivery_lat_log
end as field
from customer as c
where c.phone_number in ('254719401837', '25472054919')