有没有替代 LEFT JOIN 在自定义表中与 WordPress 的


Is there an alternative to LEFT JOIN in custom tables with WordPress?

我在WordPress数据库中有两个自定义表。

table1:  
id | title | etc  
table2:  
id | table1_id | title | source | etc

我让这个左联接完美地工作:

select p.title as ptitle, e.id, e.title, e.source, e.etc
FROM table2 AS e
LEFT JOIN table1 AS p 
ON p.id = e.table1_id 
where e.table1_id = 1

返回以下内容:

ptitle1 | id1 |title1 | source1 | etc1
ptitle1 | id2 |title2 | source2 | etc2
ptitle1 | id3 |title3 | source3 | etc3

有没有办法省略这么多重复的标题?像这样:

ptitle1 | { (id1 |title1 | source1 | etc1), (id2 |title2 | source2 | etc2), (id3...) }
ptitle2 | { null }

更新 v.2

我已经更改了查询以反映 LEFT JOIN 顺序的变化并显示object { ptitle, array( null ) }情况 - 您需要的数据位于列new_col

select 
    p.title as ptitle, 
    CONCAT('object { ', p.title, ', array( ', IFNULL(GROUP_CONCAT(CONCAT('array(', e.id, ' | ', e.title, ' | ', e.source, ' | ', e.etc, ')') SEPARATOR ', '), 'null'), ' ) }') AS new_col
FROM 
    table1 AS p
    LEFT JOIN table2 AS e  
        ON p.id = e.table1_id 
where 
    p.id = 1
GROUP BY 
    p.title

参考您的评论,new_col是一个字符串,我认为您需要这种形式的它,因为您将在此文本上应用解析器来解释它并为您返回正确的对象。对于这种情况,我认为您必须更仔细地描述情况,出于什么确切原因,您需要这种特定形式的结果,也许在wordpress api中使用某种广泛的用法 - 我不确定100%。