-1

我有一个包含这些代码的数据库

间接客户 客户代码 疗养院 NH000001 牙医 DN000001 其他 OT000001

和直接客户 客户代码 疗养院 NH050001 牙医 DN050001 其他 OT050001

注意 NH 后有 00 代表间接客户,05 代表直接客户

我需要编写一个查询来获取直接和间接客户的列表

SELECT * FROM bmwregistration ,corporation_data_entity,category_data_entity,ward_data_entity
                Where occu_corp_id='$corpo'         
                AND occu_division = '$divi' 
                AND bmwregistration.occu_corp_id = corporation_data_entity.corp_id

                AND bmwregistration.occu_ward = ward_data_entity.ward_id
                AND (cust_code LIKE '%BB00%' 
                OR cust_code LIKE '%DP00%' 
                OR cust_code LIKE '%NH00%' 
                OR cust_code LIKE '%PL00%' 
                OR cust_code LIKE '%DN00%' 
                OR cust_code LIKE '%OT00%' 
                OR cust_code LIKE '%PD00%' 
                OR cust_code LIKE '%PN00%' ) 

执行上述查询时,它会为相同的客户代码显示一行 8 次。我可以使用 group by 子句吗?查询有什么问题?

它在连续八行客户代码中选择具有相同客户代码的行

1
1
1
1
1
1
1
1
2
2
2
2
2
2
2
2
3( 8 times same row with cust_id 3)
4

1 回答 1

2

试试这个group by

   SELECT * FROM bmwregistration ,corporation_data_entity,category_data_entity,ward_data_entity
            Where occu_corp_id='$corpo'         
            AND occu_division = '$divi' 
            AND bmwregistration.occu_corp_id = corporation_data_entity.corp_id

            AND bmwregistration.occu_ward = ward_data_entity.ward_id
            AND (cust_code LIKE '%BB00%' 
            OR cust_code LIKE '%DP00%' 
            OR cust_code LIKE '%NH00%' 
            OR cust_code LIKE '%PL00%' 
            OR cust_code LIKE '%DN00%' 
            OR cust_code LIKE '%OT00%' 
            OR cust_code LIKE '%PD00%' 
            OR cust_code LIKE '%PN00%' ) 
      GROUP BY  bmwregistration
于 2013-04-05T12:46:03.567 回答