使用 SQL 查询从数据库中获取 WooCommerce 单一产品数据
Get WooCommerce single products data from database with a SQL query
我正在尝试从 woocommerce 中的 woocommerce_single_product_summary
的数据库中获取原始数据。
我需要在外部使用数据,但我无法弄清楚它是如何放置在数据库中的,我可以通过以下方式收集我需要的所有其他信息:
SELECT product.ID as product_id, product.post_title as product_name, replace(product.post_content, '"', "'") as product_content, product_sku.meta_value as product_sku, product_price.meta_value as product_price, product_weight.meta_value as product_weight
FROM clk_692135435d_wp_posts as product
LEFT JOIN clk_692135435d_wp_postmeta as product_sku ON product.ID = product_sku.post_ID
LEFT JOIN clk_692135435d_wp_postmeta as product_price ON product.ID = product_price.post_ID
LEFT JOIN clk_692135435d_wp_postmeta as product_weight ON product.ID = product_weight.post_ID
LEFT JOIN clk_692135435d_wp_postmeta as product_desc ON product.ID = product_weight.post_ID
WHERE (product.post_type = 'product' OR product.post_type = 'product_variation') AND product_sku.meta_key = '_sku' AND product_price.meta_key = '_price' AND product_weight.meta_key = '_weight'
ORDER BY product_id ASC;
要从 wp_postmeta
table 获取正确的所需元键,您必须通过 phpMyAdmin 查看数据库中的 wp_postmeta
table 给定产品 ID (post_id).
那么你最好使用这种更轻量的 SQL 查询:
SELECT p.`ID` AS product_id,
p.`post_title` AS product_name,
p.`post_excerpt` AS product_short_description,
p.`post_content` AS product_description,
Max(CASE WHEN pm.meta_key = '_sku' AND p.`ID` = pm.`post_id` THEN pm.`meta_value` END) AS product_sku,
Max(CASE WHEN pm.meta_key = '_price' AND p.`ID` = pm.`post_id` THEN pm.`meta_value` END) AS product_price,
Max(CASE WHEN pm.meta_key = '_stock_status' AND p.`ID` = pm.`post_id` THEN pm.`meta_value` END) AS product_stock_status,
Max(CASE WHEN pm.meta_key = '_stock' AND p.`ID` = pm.`post_id` THEN pm.`meta_value` END) AS product_stock_qty,
Max(CASE WHEN pm.meta_key = '_weight' AND p.`ID` = pm.`post_id` THEN pm.`meta_value` END) AS product_weight,
Max(CASE WHEN pm.meta_key = '_length' AND p.`ID` = pm.`post_id` THEN pm.`meta_value` END) AS product_length,
Max(CASE WHEN pm.meta_key = '_width' AND p.`ID` = pm.`post_id` THEN pm.`meta_value` END) AS product_width,
Max(CASE WHEN pm.meta_key = '_height' AND p.`ID` = pm.`post_id` THEN pm.`meta_value` END) AS product_height,
Max(CASE WHEN pm.meta_key = '_thumbnail_id' AND p.`ID` = pm.`post_id` THEN pm.`meta_value` END) AS product_image_id
FROM `clk_692135435d_wp_posts` AS p
INNER JOIN `clk_692135435d_wp_postmeta` AS pm
ON p.`ID` = pm.`post_id`
WHERE p.`post_type` = 'product'
AND p.`post_status` = 'publish'
GROUP BY p.`ID`
ORDER BY p.`ID` ASC;
已测试并有效。
因此您需要来自 wp_postmeta
table 的每个必需的元数据键/值对,您必须在 SQL 查询 中添加以下行(以逗号分隔):
Max(CASE WHEN pm.meta_key = '_the_meta_key' AND p.`ID` = pm.`post_id` THEN pm.`meta_value` END) AS the_column_name
其中 _the_meta_key
是所需的 meta_key,the_column_name
是所需的列名称。
我正在尝试从 woocommerce 中的 woocommerce_single_product_summary
的数据库中获取原始数据。
我需要在外部使用数据,但我无法弄清楚它是如何放置在数据库中的,我可以通过以下方式收集我需要的所有其他信息:
SELECT product.ID as product_id, product.post_title as product_name, replace(product.post_content, '"', "'") as product_content, product_sku.meta_value as product_sku, product_price.meta_value as product_price, product_weight.meta_value as product_weight
FROM clk_692135435d_wp_posts as product
LEFT JOIN clk_692135435d_wp_postmeta as product_sku ON product.ID = product_sku.post_ID
LEFT JOIN clk_692135435d_wp_postmeta as product_price ON product.ID = product_price.post_ID
LEFT JOIN clk_692135435d_wp_postmeta as product_weight ON product.ID = product_weight.post_ID
LEFT JOIN clk_692135435d_wp_postmeta as product_desc ON product.ID = product_weight.post_ID
WHERE (product.post_type = 'product' OR product.post_type = 'product_variation') AND product_sku.meta_key = '_sku' AND product_price.meta_key = '_price' AND product_weight.meta_key = '_weight'
ORDER BY product_id ASC;
要从 wp_postmeta
table 获取正确的所需元键,您必须通过 phpMyAdmin 查看数据库中的 wp_postmeta
table 给定产品 ID (post_id).
那么你最好使用这种更轻量的 SQL 查询:
SELECT p.`ID` AS product_id,
p.`post_title` AS product_name,
p.`post_excerpt` AS product_short_description,
p.`post_content` AS product_description,
Max(CASE WHEN pm.meta_key = '_sku' AND p.`ID` = pm.`post_id` THEN pm.`meta_value` END) AS product_sku,
Max(CASE WHEN pm.meta_key = '_price' AND p.`ID` = pm.`post_id` THEN pm.`meta_value` END) AS product_price,
Max(CASE WHEN pm.meta_key = '_stock_status' AND p.`ID` = pm.`post_id` THEN pm.`meta_value` END) AS product_stock_status,
Max(CASE WHEN pm.meta_key = '_stock' AND p.`ID` = pm.`post_id` THEN pm.`meta_value` END) AS product_stock_qty,
Max(CASE WHEN pm.meta_key = '_weight' AND p.`ID` = pm.`post_id` THEN pm.`meta_value` END) AS product_weight,
Max(CASE WHEN pm.meta_key = '_length' AND p.`ID` = pm.`post_id` THEN pm.`meta_value` END) AS product_length,
Max(CASE WHEN pm.meta_key = '_width' AND p.`ID` = pm.`post_id` THEN pm.`meta_value` END) AS product_width,
Max(CASE WHEN pm.meta_key = '_height' AND p.`ID` = pm.`post_id` THEN pm.`meta_value` END) AS product_height,
Max(CASE WHEN pm.meta_key = '_thumbnail_id' AND p.`ID` = pm.`post_id` THEN pm.`meta_value` END) AS product_image_id
FROM `clk_692135435d_wp_posts` AS p
INNER JOIN `clk_692135435d_wp_postmeta` AS pm
ON p.`ID` = pm.`post_id`
WHERE p.`post_type` = 'product'
AND p.`post_status` = 'publish'
GROUP BY p.`ID`
ORDER BY p.`ID` ASC;
已测试并有效。
因此您需要来自 wp_postmeta
table 的每个必需的元数据键/值对,您必须在 SQL 查询 中添加以下行(以逗号分隔):
Max(CASE WHEN pm.meta_key = '_the_meta_key' AND p.`ID` = pm.`post_id` THEN pm.`meta_value` END) AS the_column_name
其中 _the_meta_key
是所需的 meta_key,the_column_name
是所需的列名称。