为 WHERE IN 展平子选择行
Flatten subselect rows for WHERE IN
我有 table 个客户,其中有 3 列用于不同帐户的 ID,如下所示:
customers: id, account1_id, account2_id, account3_id
现在,我需要 select 来自所有这些列的 ID,以便在另一个查询的 WHERE 部分中使用,如下所示:
SELECT * FROM balances
WHERE account_id IN (
SELECT account1_id, account2_id, account3_id FROM customers
)
当然它不起作用,因为它 returns 类似于二维数组,我需要一维数组。
我尝试使用:
WHERE (`account_id`, `account_id`, `account_id`) IN (
SELECT account1_id, account2_id, account3_id FROM customers
)
但它是 AND account_id,我需要 OR。也试过:
SELECT CONCAT_WS(',' account1_id, account2_id, account3_id) AS accounts FROM customers
但是它也不起作用,因为我必须在返回的字符串中搜索。
我尝试使用 REGEXP,但无法使用 subselect 获得正确的语法,而且我不相信它会有效。
那么,如何将二维数组展平为一维数组?
SELECT balances.*
FROM balances
JOIN customers ON balances.account_id IN ( customers.account1_id,
customers.account2_id,
customers.account3_id )
或与 WHERE EXISTS 相同。
我有 table 个客户,其中有 3 列用于不同帐户的 ID,如下所示:
customers: id, account1_id, account2_id, account3_id
现在,我需要 select 来自所有这些列的 ID,以便在另一个查询的 WHERE 部分中使用,如下所示:
SELECT * FROM balances
WHERE account_id IN (
SELECT account1_id, account2_id, account3_id FROM customers
)
当然它不起作用,因为它 returns 类似于二维数组,我需要一维数组。 我尝试使用:
WHERE (`account_id`, `account_id`, `account_id`) IN (
SELECT account1_id, account2_id, account3_id FROM customers
)
但它是 AND account_id,我需要 OR。也试过:
SELECT CONCAT_WS(',' account1_id, account2_id, account3_id) AS accounts FROM customers
但是它也不起作用,因为我必须在返回的字符串中搜索。
我尝试使用 REGEXP,但无法使用 subselect 获得正确的语法,而且我不相信它会有效。
那么,如何将二维数组展平为一维数组?
SELECT balances.*
FROM balances
JOIN customers ON balances.account_id IN ( customers.account1_id,
customers.account2_id,
customers.account3_id )
或与 WHERE EXISTS 相同。