如何从数据库中选择代码

时间:2013-04-05 12:36:50

标签: mysql

我拥有一个包含此代码的数据库

间接客户                  卡斯特码     养老院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%' ) 

执行上述查询时,对于相同的cust-code,它会显示一行8次。我可以使用group by子句吗?查询有什么问题?

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

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)

1 个答案:

答案 0 :(得分: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