* SOLUTION
Multiple selects on the same table
我无法弄清楚如何正确地做到这一点。我想在一个列中查询一个表中的几个键,并使用命名
返回它们id | meta_key | meta_value |
1 | KEY1 | KEY1_VALUE |
2 | KEY2 | KEY2_VALUE |
3 | KEY3 | KEY3_VALUE |
etc.
$tq="SELECT metas.meta_value
FROM meta_table AS metas
LEFT JOIN post_table AS posts
ON metas.post_id = posts.ID
WHERE metas.meta_key IN ('KEY1', 'KEY2', 'KEY3')
AND posts.post_status = 'status'
AND posts.post_type = 'type'";
$tr=$dbc->query($tq);
var_dump($tr);
查询本身运行正常。但我需要说出KEY1',' KEY2',' KEY3'返回的数组中的等等。现在所有内容都返回" [meta_value] =>字符串",我需要它" [key1] => string"," [key2] => string"," [key3] =>字符串"等
*修改 对于我所问的问题,接缝是一个重大的误解。所以我试着澄清一下。我需要返回的数组来说KEY1有一个KEY1的键,其值对应于meta_value中的KEY1,即
array({[
[KEY1] => [KEY1_VALUE],
[KEY2] => [KEY2_VALUE]
]});
现在它看起来像这样
array({[
[meta_value] => [VALUE1],
[meta_value] => [VALUE2]
]});
所以问题是我无法在foreach中定位特定值,因为所有内容都有[meta_value]的键。
答案 0 :(得分:1)
这是一个可以准确回答我所寻找的问题。
Multiple selects on the same table
我相信我可能会过多地复杂化我的问题,或者只是无法正确解释我想要实现的目标。无论如何,谢谢你的所有答案!
答案 1 :(得分:0)
MySQL允许您使用AS
关键字
SELECT id AS Key1, meta_key AS Key2, meta_value AS Key3
FROM ...
答案 2 :(得分:0)
尝试此操作并将其更换以满足您的需求。
$tr=$dbc->query($tq);
// try this piece of code
$data['records'] = $tr->result();
$key = 0;
foreach($records as $r)
{
$meta_array = array('key'.$key => '$r');
$key++;
}
答案 3 :(得分:0)
如果我误解,请提供更多说明......
$tq="SELECT metas.meta_key,metas.meta_value ...
将返回:
[0][meta_key]='KEY1'
[0][meta_value]='KEY1_VALUE'
[1][meta_key]='KEY2'
[1][meta_value]='KEY2_VALUE'
[2][meta_key]='KEY3'
[2][meta_value]='KEY3_VALUE'
您可以在显示/处理它们时将每行中的值配对,对吗?
答案 4 :(得分:0)
夫妻俩:
如果我们可以假设每个元键都是唯一的(或至少对post_ID唯一)
SELECT
metas.post_ID,
max(CASE WHEN metas.meta_key = 'KEY1' then metas.meta_value end) as Key1,
max(CASE WHEN metas.meta_key = 'KEY2' then metas.meta_value end) as Key2,
max(CASE WHEN metas.meta_key = 'KEY3' then metas.meta_value end) as Key3,
FROM meta_table AS metas
LEFT JOIN post_table AS posts
ON metas.post_id = posts.ID
AND posts.post_status = 'status'
AND posts.post_type = 'type'
WHERE metas.meta_key IN ('KEY1', 'KEY2', 'KEY3')
GROUP BY metas.post_ID
答案 5 :(得分:0)
WooCommerce用户推荐的另一种方法是在视图中合并您的数据,然后查询视图。
我在this answer to another SO post中详细介绍,但基本结构可以在下面的SQL中看到
CREATE VIEW WP_TRANSACTIONS AS
SELECT WP_POSTMETA.Post_ID AS Post_ID, WP_POSTMETA.CustomerLN AS CustomerLN, [table_name2].CustomerFN AS CustomerFN, [table_name3].Payment_Type AS Payment_Type, [table_name4].Meta_Value AS Invoice_Total
FROM WP_POSTMETA
JOIN WP_POSTMETA AS [table_name2] ON WP_POSTMETA.Post_ID = [table_name2].Post_ID
JOIN WP_POSTMETA AS [table_name3] ON WP_POSTMETA.Post_ID = [table_name3].Post_ID
JOIN WP_POSTMETA AS [table_name4] ON WP_POSTMETA.Post_ID = [table_name4].Post_ID
WHERE WP_POSTMETA.Meta_Value = "CustomerLN"
AND [table_name2].Meta_Value = "CustomerFN"
AND [table_name3].Meta_Value = "Payment_Type"
AND [table_name4].Meta_Value = "Invoice_Total"
WHERE
子句保证每POST ID
行一行,您希望每个meta_value
作为与每个meta_key
对应的单独列。在这个例子中,我使用4个键(参见下面的WP_POSTMETA数据)。要实现这一点,必须JOIN
表格自身x-1
次(其中x
是要收集的不同密钥的数量)。
-------------------------------------------------------------------------- ID POST_ID META_KEY META_VALUE -------------------------------------------------------------------------- 0001 0001 CustomerLN Test 0002 0001 CustomerFN Tester 0003 0001 Payment_Type PayPal 0004 0001 Invoice_Total $200 0005 0002 CustomerLN Doe 0006 0002 CustomerFN John 0007 0002 Payment_Type CC-Mastercard 0008 0002 Invoice_Total $1000