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

I have two custom tables in a WordPress database.

table1:  
id | title | etc  

table2:  
id | table1_id | title | source | etc

This left connection works fine for me:

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

which returns this:

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

Is there a way to skip so many ptitle repetitions? Something like that:

ptitle1 | { (id1 |title1 | source1 | etc1), (id2 |title2 | source2 | etc2), (id3...) }
ptitle2 | { null }
+4
source share
1 answer

UPDATED v.2

I changed the query to reflect the changes in the LEFT JOIN order, as well as show the object { ptitle, array( null ) }situation - the data you need is in the columnnew_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 - , , , , . , , , , , - examam wordpress api - 100%.

+2

Source: https://habr.com/ru/post/1569487/


All Articles