在GROUPBY之前应该只有一个WHERE子句。
但是由于您使用左连接,因此在
正确的
桌子状
t2.meta_key = '_billing_first_name'
因为拒绝不匹配的行,所以会得到一个内部联接。
因此,请在ON子句中设置所有条件:
cursor.execute("
SELECT t1.ID, t1.post_date, t2.meta_value AS first_name, t3.meta_value AS last_name
FROM test_posts t1
LEFT JOIN test_postmeta t2
ON t1.ID = t2.post_id AND t2.meta_key = '_billing_first_name'
LEFT JOIN test_postmeta t3
ON t1.ID = t3.post_id AND t3.meta_key = '_billing_last_name'
GROUP BY t1.ID
ORDER BY t1.post_date DESC LIMIT 20")
尽管这个查询在语法上可能对MySql是正确的,但是使用GROUP BY是没有意义的,因为您不做任何聚合。