1

I have two mySQL tables as follows:

[product] table
P_id | Name | Quantity
1    | B    | 10
2    | C    | 15
3    | A    | 8

[attribute] table 
P_id | Name  | Quantity
1    | Black | 5
1    | Red   | 5
2    | Blue  | 6
2    | Black | 9

How can I write an SQL query so that it can show the result from the above two tables as follows: Report:

P_id | Name  | Quantity
3    | A     | 8
1    | B     | 10
1    | Black | 5
1    | Red   | 5
2    | C     | 15
2    | Black | 9
2    | Blue  | 6

These should be sorted on [Name] column, but these should be grouping on P_id column as above. By "grouping" on P_id, I mean "keeping all records with the same P_id next to each other". IS it possible to retrieve as above arrangement using a single SQL query.

4
  • 1
    To clarify, by "grouping" you mean "keeping all records with the same P_id next to each other" (instead of an actual GROUP BY clause), right?
    – Wiseguy
    Feb 14, 2012 at 15:28
  • What is the relation between those two tables? What do you want to know exactly? My guess is product.P_id is foreign key referenced by attribute.P_id. Is taht right?
    – AlexS
    Feb 14, 2012 at 15:32
  • 1
    As I understand it, the ordering is by product name for products and by related product name for attributes. At first the model seems weird but it makes kinda sense after a while.
    – voidengine
    Feb 14, 2012 at 15:34
  • 1
    @Wiseguy: Yes, by "grouping" I mean "keeping all records with the same P_id next to each other"
    – Wasim
    Feb 14, 2012 at 15:46

6 Answers 6

3
SELECT P_id, Name, Quantity FROM (
  SELECT P_id, Name, Quantity, Name as parent, 1 as level
    FROM product
  UNION
  SELECT a.P_id, a.Name, a.Quantity, p.Name as parent, 2 as level
    FROM attribute a JOIN product p ON a.P_id = p.P_id
) combined ORDER BY parent, level, Name
3
  • I believe your answer can solve my problem, but it doesn't show all records of one P_id together. I want to keep all records with the same P_id together.
    – Wasim
    Feb 14, 2012 at 15:45
  • 1
    +1 for understanding the desired order (and, of course, actually accomplishing it)
    – Wiseguy
    Feb 14, 2012 at 15:58
  • 1
    fixed and tested: 1) you need an alias for the subselect in MySQL. 2) Ordering by Name is needed to sort attributes with the same P_id. Now it gives exactly the results desired.
    – voidengine
    Feb 15, 2012 at 14:43
0

It should be Union operation, I believe like this:

select * from product union select * from attribute order Name;

Not sure why you wrote you need `group by' since your output is not grouped by any column.

1
  • 1
    I don't see grouping either in the example in the first post.
    – koen
    Feb 14, 2012 at 15:23
0

Do you mean that you just need an union of both tables? You can try this:

Select P_id, Name, Quantity
From product

Union

Select P_id, Name, Quantity
From attribute

Order by 2
2
  • wasimchy says he wants grouping, but there is no grouping necessary.
    – koen
    Feb 14, 2012 at 15:25
  • According to your example, no group by is necessary, only an union. The amount of products is already on the table.
    – Schiavini
    Feb 14, 2012 at 15:25
0
SELECT * FROM 
(
   SELECT P_id, Name , Quantity FROM product
   UNION ALL
   SELECT P_id, Name , Quantity FROM attribute
) Order by Name
1
  • SELECT * and the () will not be necessary
    – Schiavini
    Feb 14, 2012 at 15:26
0

You will have to use grouping my friend. Then you can order on Both of you required columns like this:

SELECT * FROM product UNION SELECT * FROM attribute ORDER BY Name, P_id;
0

If you want them ordered by P_id and then Name, this should work.

(SELECT P_id, Name, Quantity FROM product)
UNION
(SELECT P_id, Name, Quantity FROM attribute)
ORDER BY P_id, Name;

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.