Wanjala Alex Wanjala Alex - 2 years ago 94
MySQL Question

Combine SQL statement

I would like to select different column from different row from the same table with one statement . How will I combine this to be one?

SELECT `shop_lat_log` FROM `customers` WHERE `phoneNumber`='254719401837'
SELECT `delivery_lat_log` FROM `customers` WHERE `phoneNumber`='25472054919'

Answer Source

You could just use each of two current queries as subqueries in one select statement:

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;

This assumes that each of your two queries returns a single value. If not, then perhaps a UNION would be more appropriate:

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'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download