1

我需要获取重复数据的空字段

例如一个客户可以有两个或多个联系人,所以查询返回(只是短了qyery resul):

客户名称| CONTACT_PERSON|ETC..

  dell         |    Ighor      |etc..
  dell         |    Dima       |etc..

但我需要: CUSTOMER_NAME| CONTACT_PERSON|等等...

             dell         |    Ighor      |etc..
             NULL         |    Dima       |etc..


SELECT
             `contact`.*,
                 `branch_has_equipment`.*,
             `branch_has_contact`.*,
                         `equipment`.*,
                         `customer_has_branch`.*,
                         `branch`.*,
                         `customer`.*,
              `ip`.*

                 FROM `customer`
                 INNER JOIN `customer_has_branch`
                         ON `customer`.`customer_id` = `customer_has_branch`.`customer_id`
                 INNER JOIN `branch`
                         ON `customer_has_branch`.`branch_id` = `branch`.`branch_id`
                 INNER JOIN `branch_has_equipment`
                         ON `branch`.`branch_id` = `branch_has_equipment`.`branch_id`
                 INNER JOIN `equipment`
                         ON `branch_has_equipment`.`equipment_id` = `equipment`.`equipment_id`
                 INNER JOIN `branch_has_contact`
                         ON `branch`.`branch_id` = `branch_has_contact`.`branch_id`
                 INNER JOIN `contact`
                         ON `branch_has_contact`.`contact_id` = `contact`.`contact_id`

                INNER JOIN `equipment_has_ip`
                        ON `equipment`.`equipment_id` = `equipment_has_ip`.`equipment_id`
                INNER JOIN `ip`
                        ON `equipment_has_ip`.`equipment_id` = `ip`.`ip_id`  

                 WHERE `customer`.`inservice` = 'Yes'
                         ORDER BY `customer`.`customer_name`

另外,表格^

顾客

customer_id customer_name inservice service_type 评论

分支

branch_id 城市地址

设备

设备_id 品牌型号 连接参数 连接类型 序列号 id 发布

接触

联系人_id 姓名 姓 电话_手机_工作电话_其他职位

customer_has_branch_id customer_id branch_id

4

2 回答 2

0

由于我不知道这些表中的任何一个是如何相互关联的,因此我对您的唯一回答是使用OUTER JOIN,它将保留 NULL 结果。

于 2013-03-19T15:25:02.990 回答
0

我并不认真地提倡这种解决方案,因为我真的认为这种事情应该在应用程序级代码中处理(例如一点 PHP),但无论如何,请考虑以下事项:

 SELECT * FROM my_table;
 +------+--------+--------+
 | game | points | player |
 +------+--------+--------+
 |    1 |      5 | Adam   |
 |    1 |      8 | Alan   |
 |    1 |      7 | Brian  |
 |    1 |      6 | John   |
 |    2 |      2 | Adam   |
 |    2 |      3 | Alan   |
 |    2 |      4 | Brian  |
 |    2 |      6 | John   |
 +------+--------+--------+

 SELECT IF(game= @prev,'',game)
      , @prev := game
   FROM my_table ORDER BY game,player;
 +-------------------------+---------------+
 | IF(game= @prev,'',game) | @prev := game |
 +-------------------------+---------------+
 | 1                       |             1 |
 |                         |             1 |
 |                         |             1 |
 |                         |             1 |
 | 2                       |             2 |
 |                         |             2 |
 |                         |             2 |
 |                         |             2 |
 +-------------------------+---------------+
于 2013-03-19T17:08:42.270 回答