MySQL Join Query没有重复值

时间:2016-05-20 11:18:46

标签: mysql

  1. 发票表
  2.  SELECT id, fname, gtotal, `date` FROM invoice WHERE id = 1;
    
    
    | id |   fname | gtotal |                  date |
    |----|---------|--------|-----------------------|
    |  1 | Brandon |    860 | May, 11 2016 00:00:00 |
    
    1. invoice_contents表,
    2. SELECT * FROM invoice_contents WHERE invoice_id = 1;
      
      
      | id | invoice_id |       item | price | quantity | discount | total |
      |----|------------|------------|-------|----------|----------|-------|
      |  1 |          1 |   Dextrose |    10 |       10 |        5 |    95 |
      |  2 |          1 |   Nescaine |    20 |       30 |       10 |   540 |
      |  3 |          1 | Anticavity |    30 |       10 |       25 |   225 |
      
      1. 此JOIN查询
      2. SELECT invoice.id, invoice.fname, invoice_contents.item,
               invoice_contents.price, invoice_contents.quantit,
               invoice_contents.discount, invoice_contents.total, 
               invoice.gtotal
          FROM invoice_contents
         INNER JOIN invoice ON invoice_contents.invoice_id=1 AND invoice.id=1;
        

        给出了这个结果。

         | id |   fname |       item | price | quantity | discount | total | gtotal |
         |----|---------|------------|-------|----------|----------|-------|--------|
         |  1 | Brandon |   Dextrose |    10 |       10 |        5 |    95 |    860 |
         |  1 | Brandon |   Nescaine |    20 |       30 |       10 |   540 |    860 |
         |  1 | Brandon | Anticavity |    30 |       10 |       25 |   225 |    860 |
        

        我需要这个结果。

        | id |   fname |       item | price | quantity | discount | total | gtotal |
        |----|---------|------------|-------|----------|----------|-------|--------|
        |  1 | Brandon |   Dextrose |    10 |       10 |        5 |    95 |    860 |
        |    |         |   Nescaine |    20 |       30 |       10 |   540 |        |
        |    |         | Anticavity |    30 |       10 |       25 |   225 |        |
        

        我只是MySQL的初学者。我一直在尝试从今天早上通过试验不同的组合获得这种输出,请帮助我。

2 个答案:

答案 0 :(得分:2)

@Rex,你的选择是正确的。您应该使用某些脚本制作所需的输出,例PHP。

答案 1 :(得分:1)

在SQL中尝试:

在这个查询中我保存每次变量中的fname不相等而在下一行我比较它并返回一个空字符串是否相等。和gtotal一样。

交叉连接仅用于初始化变量。

在这种情况下,重要的是行按fname排序,以确保相同的名称在彼此后面

SELECT
    invoice.id,
    IF(@last_fname = invoice.fname, '', (@last_fname:=invoice.fname)) as fname,
    invoice_contents.item,
    invoice_contents.price,
    invoice_contents.quantity,
    invoice_contents.discount,
    IF(@last_gtotal = invoice.gtotal, '', (@last_gtotal:=invoice.gtotal)) as gtotal
FROM invoice_contents
INNER JOIN invoice ON invoice_contents.invoice_id=1 AND invoice.id=1
CROSS JOIN ( select @last_fname := '' , @last_gtotal := '' ) AS parameter
ORDER BY invoice.fname;

<强>示例

MariaDB [bb]> SELECT
    ->     invoice.id,
    ->     IF(@last_fname = invoice.fname, '', (@last_fname:=invoice.fname)) AS fname,
    ->     invoice_contents.item,
    ->     invoice_contents.price,
    ->     invoice_contents.quantity,
    ->     invoice_contents.discount,
    ->     IF(@last_gtotal = invoice.gtotal, '', (@last_gtotal:=invoice.gtotal)) AS gtotal
    -> FROM invoice_contents
    -> INNER JOIN invoice ON invoice_contents.invoice_id=1 AND invoice.id=1
    -> CROSS JOIN ( SELECT @last_fname:='' , @last_gtotal:='' ) AS parameter
    -> ORDER BY invoice.fname;
+----+---------+------------+-------+----------+----------+--------+
| id | fname   | item       | price | quantity | discount | gtotal |
+----+---------+------------+-------+----------+----------+--------+
|  1 | Brandon | Dextrose   | 10.00 |       10 |     5.00 | 860.00 |
|  1 |         | Nescaine   | 20.00 |       30 |    10.00 |        |
|  1 |         | Anticavity | 30.00 |       10 |    25.00 |        |
+----+---------+------------+-------+----------+----------+--------+
3 rows in set, 1 warning (0.00 sec)

MariaDB [bb]>