mysql multi group by

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Free User
    New Member
    • Dec 2010
    • 3

    #1

    mysql multi group by

    Hello ,
    I need a small help with multi GROUP BY .

    Mysql example table with name 'mytable' has :

    id - modelid - nameid
    _______________ ___________

    1 | 1 | 2
    2 | 5 | 6
    3 | 3 | 11
    4 | 2 | 10
    5 | 6 | 1
    6 | 7 | 1
    7 | 5 | 1
    8 | 8 | 1

    I need to display number of count(modelid) and count(nameid) in one query . Is possible to do with GROUP BY. Please show me an query example with this resul:

    modelid:5 count:2
    modelid:1 count:1
    modelid:3 count:1
    modelid:2 count:1
    modelid:6 count:1
    modelid:7 count:1
    modelid:8 count:1

    nameid:1 count 4
    nameid:2 count 1
    nameid:6 count 1
    nameid:11 count 1
    nameid:10 count 1

    Thank You!
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    You can't do this in one query. You can do it in two separate statements that you union together.

    Comment

    • Free User
      New Member
      • Dec 2010
      • 3

      #3
      Thank You for your reply Rabbit,

      I'm afraid that I will lose performance because I need to search multi rows with MATCH AGAINST.

      The goal is to extract result from database like newegg.com :P and I need to multi query MATCH AGAINST and GROUP BY .

      Exist any other way with just mysql and php?

      Thank You again!

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        Match against doesn't require a group by nor a count so I don't know where you're going with this.

        Comment

        • Free User
          New Member
          • Dec 2010
          • 3

          #5
          Yes I know ,

          Just in my small project I need to group result like newegg do , with filter_items and count(filter_it ems).

          My question was a simple table. In real database my query use match against and group by and for each type of filter_items is necessary to re-query the database.

          My example was really stupid :P

          To have an idea just search on newegg.com for "tv" and you will see on the left filter items : SubCategory, Useful Links, Price etc. with count for each one.

          You have right , is not possible in one query.

          Comment

          Working...