Я все. У меня есть массив запросов

$queries = [
    $sql1,
    $sql2,
    $sql3,
    $sql4
];

Мне нужна функция PHP, которая может автоматически объединять «UNION» между ними. Например, если есть все 4 запроса, он должен вернуть что-то вроде

$sql1 . ' UNION ' . $sql2 . ' UNION ' . $sql3 . ' UNION ' . $sql4;

Если $sql2 и $sql3 пусты, он должен вернуть

$sql1 . ' UNION ' . $sql4

В настоящее время я использую эту функцию

function concatenate_queries($arr){
    $final_query = "";
    foreach ($arr as $q){
        $final_query = $q . ' UNION ';
    }
    return $final_query;
}

Но это не дает правильного вывода.

< Сильный > ИЗМЕНИТЬ

Пример запросов

<pre>Array
(
    [0] => SELECT 'deposit' as t_type, tx_amount as total, tx_fee as fee, tx_net as amount, tx_currency as currency, tx_exchange_rate as ex_rate, transactionTime as created , paymentId as trax_id,  'payments' as tb, 'completed' as status  FROM payments WHERE userId='51' AND (DATE(transactionTime) BETWEEN '2018-08-24' AND '2018-09-24')
UNION
SELECT 'deposit' as t_type, amount as total, 0 as fee, amount as amount, 'EUR' as currency, 1 as ex_rate,  created, id as trax_id,  'coinbase_deposits' as tb, 'completed' as status FROM coinbase_deposits WHERE asset='EUR' AND userId='51' AND (DATE(created) BETWEEN '2018-08-24' AND '2018-09-24')

    [1] => SELECT 'buy' as t_type, total_amount as total, fee_coinbase as fee, subtotal as amount, total_amount_currency as currency, 1 as ex_rate,  created, id as trax_id, 'coinbase_transactions' as tb,status FROM coinbase_transactions
WHERE wallet='EUR' AND userId='51' AND resource='sell' AND (DATE(created) BETWEEN '2018-08-24' AND '2018-09-24') 

    [2] => SELECT 'withdraw' as t_type, amount as total, 0 as fee, amount, 'gbp' as currency , 1 as ex_rate, created, id as trax_id, 'refunds' as tb, 'completed' as status FROM refunds WHERE user_id='51' AND (DATE(created) BETWEEN '2018-08-24' AND '2018-09-24') 
UNION
 SELECT 'withdraw' as t_type, amount as total_amount, 0 as fee, amount,  'gbp' as currency , 1 as ex_rate, created, id as trax_id, 'manual_refunds' as tb,  CASE WHEN (status=0) THEN 'pending' ELSE 'completed' END as status FROM manual_refunds WHERE user_id='51' AND (DATE(created) BETWEEN '2018-08-24' AND '2018-09-24
    [3] => SELECT 'sell' as t_type, total_amount as total, fee_coinbase as fee, subtotal as amount, total_amount_currency as currency, 1 as ex_rate, created , id as trax_id, 'coinbase_transactions' as tb, status FROM coinbase_transactions WHERE wallet='EUR' AND userId='51' AND resource='buy' AND (DATE(created) BETWEEN '2018-08-24' AND '2018-09-24')

)
</pre>
0
Ali Zia 24 Сен 2018 в 10:05

2 ответа

Лучший ответ

Вы пробовали implode()?

$final_query = implode(' UNION ', array_filter($queries));
5
fabrik 24 Сен 2018 в 07:31

Если вы сделаете это по-своему, у вас будет UNION после последней строки кода, поэтому вы получите sql1 UNION sql2 UNION sql3 UNION sql4 UNION. Это не удастся.

Вы могли бы сделать что-то вроде этого:

function concatenate_queries($arr){
    $final_query = "";
    $union = "";
    foreach ($arr as $q){
        $final_query .= $union . $q;
        $union = ' UNION ';
    }
    return $final_query;
}

Таким образом, у вас будет sql1 UNION sql2 UNION sql3 UNION sql4

РЕДАКТИРОВАТЬ: способ Fabric более эффективен, просто используйте implode ().

0
Rolfie 24 Сен 2018 в 07:14