为每个其他所选项目输出4个选定项目

时间:2014-10-12 10:23:59

标签: php mysql select

大家好!我需要从mysql数据库中选择正确数量的特定项目。

这是我的架构 - http://i.stack.imgur.com/Jr7ky.jpg

这是我的问题:

$servBasketData = mysql_query("SELECT services_basket.sbasket_id, system_countries.country_name, system_services.service_name, system_podservice.podservice_name, system_packets.packet_name, system_packets.price_common, system_packets.price_econom, system_packets.price_standart, system_packets.price_vip, packets_basket.sbasket_id
    FROM system_services INNER JOIN (system_podservice INNER JOIN (system_packets INNER JOIN (system_countries INNER JOIN (services_basket INNER JOIN packets_basket ON services_basket.sbasket_id = packets_basket.sbasket_id) ON system_countries.country_id = services_basket.country_id) ON system_packets.packet_id = packets_basket.packet_id) ON system_podservice.podservice_id = services_basket.podservice_id) ON system_services.service_id = services_basket.service_id ORDER BY services_basket.sbasket_id ASC;");

    while ($result = mysql_fetch_array($servBasketData)) {
        echo "<tr><td><center>".$result['sbasket_id']."</center></td><td><center>".$result['country_name']."</center></td><td><center>".$result['service_name']."</center></td><td><center>".$result['podservice_name']."/<center></td><td>";
                     echo "<tr><td><center>".$result['packet_name']."</center></td><td><center>".$result['price_common']."</center></td><td><center>".$result['price_econom']."</center></td><td><center>".$result['price_standart']."</center></td><td><center>".$result['price_vip']."</center></td>";


    }

此代码返回的数据如下:

1 |俄罗斯| service1 | podservice1 | packet1 | price_common_20 | price_econom_30 | price_standart_40 | price_vip_50

1 |俄罗斯| service1 | podservice1 | packet2 | price_common_30 | price_econom_40 | price_standart_50 | price_vip_60

1 |俄罗斯| service1 | podservice1 | packet3 | price_common_40 | price_econom_50 | price_standart_60 | price_vip_70

1 |俄罗斯| service1 | podservice1 | packet4 | price_common_50 | price_econom_60 | price_standart_70 | price_vip_80


2 |乌克兰| service1 | podservice1 | packet1 | price_common_20 | price_econom_30 | price_standart_40 | price_vip_50

2 |乌克兰| service1 | podservice1 | packet2 | price_common_30 | price_econom_40 | price_standart_50 | price_vip_60

2 |乌克兰| service1 | podservice1 | packet3 | price_common_40 | price_econom_50 | price_standart_60 | price_vip_70

2 |乌克兰| service1 | podservice1 | packet4 | price_common_50 | price_econom_60 | price_standart_70 | price_vip_80


...

所以,这不是我需要得到的。我需要获得这样的数据:

1 |俄罗斯| service1 | podservice1 |

packet1 | price_common_20 | price_econom_30 | price_standart_40 | price_vip_50

packet2 | price_common_30 | price_econom_40 | price_standart_50 | price_vip_60

packet3 | price_common_40 | price_econom_50 | price_standart_60 | price_vip_70

packet4 | price_common_50 | price_econom_60 | price_standart_70 | price_vip_80


2 |乌克兰| service1 | podservice1 |

packet1 | price_common_20 | price_econom_30 | price_standart_40 | price_vip_50

packet2 | price_common_30 | price_econom_40 | price_standart_50 | price_vip_60

packet3 | price_common_40 | price_econom_50 | price_standart_60 | price_vip_70

packet4 | price_common_50 | price_econom_60 | price_standart_70 | price_vip_80


所以我需要删除重复的数据。 我怎么能这样做? (没关系 - 通过php或mysql)

谢谢。

1 个答案:

答案 0 :(得分:0)

第一个和第二个输出之间的唯一区别是第二个输出不包括sbasket_idservice_namepodservice_name

您的问题可以通过使用php来控制输出的内容以及取决于sbacket_id何时更改时的问题。

快速示例(借口错误)

    $servBasketData = mysql_query("

    SELECT services_basket.sbasket_id,
           system_countries.country_name,
           system_services.service_name,
           system_podservice.podservice_name,
           system_packets.packet_name,
           system_packets.price_common,
           system_packets.price_econom,
           system_packets.price_standart,
           system_packets.price_vip,
           packets_basket.sbasket_id
    FROM system_services
    INNER JOIN (system_podservice
                INNER JOIN (system_packets
                INNER JOIN (system_countries
                INNER JOIN (services_basket
                INNER JOIN packets_basket ON 
    services_basket.sbasket_id = packets_basket.sbasket_id) ON 
    system_countries.country_id = services_basket.country_id) ON 
    system_packets.packet_id = packets_basket.packet_id) ON 


       system_podservice.podservice_id = services_basket.podservice_id) ON 
        system_services.service_id = services_basket.service_id
        ORDER BY services_basket.sbasket_id ASC;");

        $previous_sbasket_id = -1; 
        while ($result = mysql_fetch_array($servBasketData)) 
        {

    if($previous_sbasket_id != $result['sbasket_id'])
    {
//echo here the header if needed
echo "<tr><td><center>".$result['sbasket_id']."</center></td><td><center>".$result['country_name']."</center></td><td><center>".$result['service_name']."</center></td><td><center>".$result['podservice_name']."/<center></td><td>";

    }

//echo here everything else

    echo "<tr><td><center>".$result['packet_name']."</center></td><td><center>".$result['price_common']."</center></td><td><center>".$result['price_econom']."</center></td><td><center>".$result['price_standart']."</center></td><td><center>".$result['price_vip']."</center></td>";

    $previous_sbasket_id= $result['sbasket_id'];

    }