douao7937 2015-11-12 13:27
浏览 44

PHP / mySQL连接 - 结果中有多个重复的行

In a wordpress based booking system I am trying to find the number of Adults that are going to all events for all time - this info is stored in the wp_woocommerce_order_itemmeta table (e.g. meta_key='Adults', meta_value='6') . In phpmyadmin I can see in the wp_woocommerce_order_itemmeta table there are 20 rows which have a meta_key value of 'Adults' (ie 20 total bookings). But when I run this code rather than printing 20 lines, each line gets repeated approx 42 times, so i end up with 840 lines (or so)

e.g.

meta_key Adults meta_value 6 meta_key Adults meta_value 6 meta_key Adults meta_value 6 meta_key Adults meta_value 6 meta_key Adults meta_value 6 meta_key Adults meta_value 6 meta_key Adul .. and so on

rather than:

meta_key Adults meta_value 6 meta_key Adults meta_value 8 meta_key Adults meta_value 11 meta_key Adults meta_value 1 meta_key Adults meta_value 1 meta_key Adults meta_value 2 .. and so on up to the desired 20 pairs of results

This is the code:

global $wpdb;
$results= $wpdb->get_results( '
select wp_woocommerce_order_itemmeta.meta_key, wp_woocommerce_order_itemmeta.meta_value
FROM wp_postmeta
inner JOIN wp_posts
ON wp_postmeta.post_id = wp_posts.ID
inner JOIN wp_woocommerce_order_items
ON wp_woocommerce_order_items.order_id = wp_posts.ID
inner JOIN wp_woocommerce_order_itemmeta
ON wp_woocommerce_order_itemmeta.order_item_id = wp_woocommerce_order_items.order_item_id
where 1=1
AND wp_woocommerce_order_itemmeta.meta_key ="Adults"
', OBJECT );
foreach ($results as $res2) {
foreach ($res2 as $key => $value) {
            echo "$key $value
";
            }
}

If I add DISTINCT to the SELECT then I lose rows where the number of adults going is the same, so that's not what I want. I just want to print those 20 rows that exist then I can add up the number of Adults going to all events for all time.

I need the joins in there as later I will first have to check that a booking is complete (which is in the wp_posts table), and I need to relate the post ID to the order_ID to relate that, etc etc

Where is my logic failing at this early stage? Thanks for any help.

  • 写回答

1条回答 默认 最新

  • dongshuxi3105 2015-11-12 13:38
    关注

    I not sure that I fully understand your SQL, but can you use LEFT JOIN instead INNER JOIN?

    select wp_woocommerce_order_itemmeta.meta_key, wp_woocommerce_order_itemmeta.meta_value
    FROM wp_postmeta
    LEFT JOIN wp_posts
    ON wp_postmeta.post_id = wp_posts.ID
    LEFT JOIN wp_woocommerce_order_items
    ON wp_woocommerce_order_items.order_id = wp_posts.ID
    LEFT JOIN wp_woocommerce_order_itemmeta
    ON wp_woocommerce_order_itemmeta.order_item_id = wp_woocommerce_order_items.order_item_id
    where 1=1
    AND wp_woocommerce_order_itemmeta.meta_key ="Adults"
    

    I think it's will help you. Also read please different between JOINs.

    If it willn't help get except result, you can try goup your results by GROUP BY

    select wp_woocommerce_order_itemmeta.meta_key, wp_woocommerce_order_itemmeta.meta_value
    FROM wp_postmeta
    inner JOIN wp_posts
    ON wp_postmeta.post_id = wp_posts.ID
    inner JOIN wp_woocommerce_order_items
    ON wp_woocommerce_order_items.order_id = wp_posts.ID
    inner JOIN wp_woocommerce_order_itemmeta
    ON wp_woocommerce_order_itemmeta.order_item_id = wp_woocommerce_order_items.order_item_id
    where 1=1
    AND wp_woocommerce_order_itemmeta.meta_key ="Adults"
    GROUP BY wp_posts.ID
    
    评论

报告相同问题?

悬赏问题

  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线