Share Pastebin
Guest
Public paste!

p

By: a guest | Mar 13th, 2010 | Syntax: None | Size: 2.19 KB | Hits: 57 | Expires: Never
This paste has a previous version, view the difference. Copy text to clipboard
  1. This is the table.
  2.  
  3. +-------+----------+--------------------------------+-----+---------+------------+
  4. | catid | parentid | catname                        | pos | enabled | timestamp  |
  5. +-------+----------+--------------------------------+-----+---------+------------+
  6. |     1 |        0 | Stuff for Sale                 |   1 | 1       | 1268488086 |
  7. |    13 |        1 | Baby & Kids Stuff              |   3 | 1       | 1268490153 |
  8. |   121 |       13 | Baby Clothes                   |   1 | 1       | 1268492200 |
  9. |   122 |       13 | Baby Toys                      |   2 | 1       | 1268492200 |
  10. |   123 |       13 | Car Seats & Baby Carriers      |   3 | 1       | 1268492201 |
  11. |   124 |       13 | Nursery & Children's Furniture |   4 | 1       | 1268492201 |
  12. |   125 |       13 | Prams & Strollers              |   5 | 1       | 1268492201 |
  13. |   126 |       13 | Other                          |   6 | 1       | 1268492201 |
  14. +-------+----------+--------------------------------+-----+---------+------------+
  15.  
  16. When I run with a subselect:
  17.  
  18. mysql> select catid, (select catname from cats where catid=1) as toplevel, (select catname from cats where catid=13) as
  19. parent, catname, pos from cats c where c.parentid=13;
  20.  
  21. I get this:
  22.  
  23. +-------+----------------+-------------------+--------------------------------+-----+
  24. | catid | toplevel       | parent            | catname                        | pos |
  25. +-------+----------------+-------------------+--------------------------------+-----+
  26. |   121 | Stuff for Sale | Baby & Kids Stuff | Baby Clothes                   |   1 |
  27. |   122 | Stuff for Sale | Baby & Kids Stuff | Baby Toys                      |   2 |
  28. |   123 | Stuff for Sale | Baby & Kids Stuff | Car Seats & Baby Carriers      |   3 |
  29. |   124 | Stuff for Sale | Baby & Kids Stuff | Nursery & Children's Furniture |   4 |
  30. |   125 | Stuff for Sale | Baby & Kids Stuff | Prams & Strollers              |   5 |
  31. |   126 | Stuff for Sale | Baby & Kids Stuff | Other                          |   6 |
  32. +-------+----------------+-------------------+--------------------------------+-----+
  33. 6 rows in set (0.00 sec)
  34.  
  35. mysql>
  36.  
  37. How can I achieve the same result without using subselects to get the same result.