3
  1. invoice table
 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 table,
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. This JOIN query
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;

gives this result.

 | 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 |

I need this result.

| 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 |        |

I am just a beginner in MySQL. I have been trying from this morning to get this kind of output by experimenting on different combinations please help me out.

3
  • SQL Fiddle: sqlfiddle.com/#!9/b29412/13 Commented May 20, 2016 at 11:30
  • It's odd that no relationship exists between these tables. But, putting that to one side, you're confusing data retrieval and data display - the latter being more properly addressed in application level code (e.g. a simple php loop) Commented May 20, 2016 at 11:44
  • Though not explicitly set in MySQL, the relationship is created by assigning the same number as the id field in the invoice table to the invoice_contents table's invoice_id field (courtesy: php's mysqli_insert_id) Commented May 20, 2016 at 11:56

2 Answers 2

2

@Rex, Your select is correct. You should make desired output using some script e.g. PHP.

Sign up to request clarification or add additional context in comments.

1 Comment

That was my gut feeling too. This is ultimately intended for a PHP MySQL application.
1

try this in SQL:

in this Query i save everytime fname in a variable is not equal and at the next row i compare it and return a empty string is it equal. and the same for gtotal.

the cross join is only to initialize the variables.

in this case it is important that the rows are order by fname to ensure that the same name is behind each other

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;

Sample

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]>

7 Comments

Because the original poster is a beginner, may I suggest you edit this answer to explain what's going on?
Thanks. Running your query in SQLFIDDLE demo shows the desired output. But in my wamp setup using both mysql cli and adminer.php (similar to phpmyadmin) I get the duplicate values
@Rex - can you please post some output
Working SQL Fiddle: sqlfiddle.com/#!9/b29412/15 Not the expected output - postimg.org/image/twnwnvy2p
Ran the same query in MySQL CLI but got this output postimg.org/image/twnwnvy2p. Also tried in my Linux LAMP setup got the same postimg.org/image/jsrm20jwh
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.