我在接下来的几周内遇到了一些问题。我已经阅读了我发现的所有教程和片段,但无法完成这项工作。
我有下表(wordpress数据库):
if input_0 == "Start" or "start": #These should evaluate the users input
Waking_up ()
我需要所有订单中所有人的清单。列表应如下所示。
post_id meta_key meta_value
802 _billing_first_name John
802 _billing_last_name Johnson
802 _billing_first_name2 Jack
802 _billing_last_name2 Jackson
802 _billing_first_name3 Jason
802 _billing_last_name3 Jasonson
803 _billing_first_name Jamie
803 _billing_last_name Jameson
803 _billing_first_name2 Oliver
803 _billing_last_name2 Olverson
每个订单的名称可能无限制。一个订单可能是20人,而下一个可能是一个人。
答案 0 :(得分:3)
您可以使用自我加入:
SELECT t1.post_id AS ID, t1.meta_value AS Firstname, t2.meta_value AS Lastname
FROM tab t1
JOIN tab t2
ON t1.post_id = t2.post_id
AND t1.meta_key LIKE '_billing_first_name%'
AND t2.meta_key LIKE '_billing_last_name%'
AND RIGHT(t1.meta_key, 3) = RIGHT(t2.meta_key,3); -- up to 999 users
的 LiveDemo
强>
输出:
╔═════╦═══════════╦══════════╗
║ ID ║ FirstName ║ LastName ║
╠═════╬═══════════╬══════════╣
║ 802 ║ John ║ Johnson ║
║ 802 ║ Jack ║ Jackson ║
║ 802 ║ Jason ║ Jasonson ║
║ 803 ║ Jamie ║ Jameson ║
║ 803 ║ Oliver ║ Olverson ║
╚═════╩═══════════╩══════════╝
关键是MySQL
是关系数据库,并且与EAV
设计不兼容。这个解决方案在非常大的表中可能很慢,因为连接条件是non-SARGable
。
修改强>
我猜你想加入:
带有_billing_first_name2
的 _billing_email002
您可以使用普通数字来处理000
,但效果会很差:
SELECT t1.post_id AS ID, t1.meta_value AS Firstname, t2.meta_value AS Email
FROM tab t1
JOIN tab t2
ON t1.post_id = t2.post_id
AND t1.meta_key LIKE '_billing_first_name%'
AND t2.meta_key LIKE '_billing_last_email%' --email
AND CONCAT(
IF(SUBSTRING(t1.meta_key,-3,1) REGEXP '[0-9]',SUBSTRING(t1.meta_key,-3,1), '0'),
IF(SUBSTRING(t1.meta_key,-2,1) REGEXP '[0-9]',SUBSTRING(t1.meta_key,-2,1), '0'),
IF(SUBSTRING(t1.meta_key,-1,1) REGEXP '[0-9]',SUBSTRING(t1.meta_key,-1,1), '0')
) =
CONCAT(
IF(SUBSTRING(t2.meta_key,-3,1) REGEXP '[0-9]',SUBSTRING(t2.meta_key,-3,1), '0'),
IF(SUBSTRING(t2.meta_key,-2,1) REGEXP '[0-9]',SUBSTRING(t2.meta_key,-2,1), '0'),
IF(SUBSTRING(t2.meta_key,-1,1) REGEXP '[0-9]',SUBSTRING(t2.meta_key,-1,1), '0')
)