我正在尝试输出一个表格,其中包含产品所需的所有内容以显示在网页中,然后在其他地方的另一个表格中重新组织以供引用。
我有my_posts、my_postmeta 表格。
在 my_posts 表中,我有 post_type 是 'product ' 和产品名称 post_title。我还有产品的 ID。
然后我有另一个表my_postmeta,它在同一列中有两个值my_postmeta.meta_key 是 '_sku' 和 '_regular_price'。
我做了一个可以打印这样的查询:
查询:
SELECT my_posts.post_title,
my_posts.post_type,
my_posts.ID,
GROUP_CONCAT(my_postmeta.meta_value)
FROM my_posts
INNER JOIN
my_postmeta
ON
my_posts.ID=my_postmeta.post_id
WHERE
my_posts.post_type
LIKE 'product'
AND
my_postmeta.meta_key IN ('_sku', '_regular_price')
GROUP BY
my_posts.post_title
但我想在不同的列中分隔值“_sku”、“_regular_price”。 像这样:
有没有可能做这样的事情?
最佳答案
你应该在 my_postmeta 上使用两个连接而不是 group_concat
SELECT t1.post_title,
t1.post_type,
t1.ID,
m1.meta_value as regular_price,
m2.meta_value as sku,
FROM my_posts t1
INNER JOIN my_postmeta m1 ON t1.ID=m1.post_id and m1.meta_key= '_regular_price'
INNER JOIN my_postmeta m2 ON t1.ID=m2.post_id and m2.meta_key= '_sku'
WHERE t1.post_type LIKE 'product'
关于php - 使用 PHP PDO 将同一 mysql 列中的值打印到多个列中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44706680/