由于数据库的组织方式,我无法将正确的结果集放入 1 行。我使用以下 SQL:
SELECT ord.ID, ord.post_date, ord.post_status, ord.post_type,
meta.meta_value, term.term_id, term.name,
SUBSTRING(meta.meta_value,LOCATE('\"id\";i:', meta.meta_value)+7,3) AS prodID,
user.user_email
FROM wp_posts ord
INNER JOIN wp_postmeta meta ON meta.post_id = ord.ID
INNER JOIN wp_term_relationships rel ON ord.ID = rel.object_id
INNER JOIN wp_terms term on term.term_id = rel.term_taxonomy_id
LEFT JOIN wp_users user ON user.ID = meta.meta_value
WHERE ord.post_type = 'shop_order'
AND (meta.meta_key = '_customer_user' OR meta.meta_key = '_order_items')
AND ord.post_date >= DATE_SUB(NOW(), INTERVAL 2 HOUR)
AND ord.post_status <> 'trash'
AND term.term_id = 34
ORDER BY `ord`.`ID` DESC
结果是:
ID Descending post_date post_status post_type meta_value term_id name prodID user_email
451 2013-02-02 10:24:00 publish shop_order 2 34 processing sales@proxyplayer.co.uk
451 2013-02-02 10:24:00 publish shop_order a:1:{i:0;a:10:{s:2:"id";s:3:"339";s:12:"variation_... 34 processing 338 NULL
理想情况下,我想将 meta.meta_value 作为一行返回,因此,元值 2 将与 a:1:{i:0;a:10:{s:2:"id";i 连接:338;s:12:"variation_id";...
但是,我认为在这种情况下我不能这样做,因为 meta_value 在元表中有 2 个单独的行。
所以,我想我可以在 PHP 中控制它并得到值 2 和值 338。但是我怎样才能在一个while循环中做到这一点?
while ($squid = mysql_fetch_array($result, MYSQL_ASSOC)) {
//1st row
//2nd row
//processing code
}
我要在其中添加一个额外的while循环来跳过数据库中每2行数据吗?我应该将它加载到数组中并循环遍历数组中的 2 行吗?