Mysql Query grouping and having clause

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Noorain
    New Member
    • Mar 2008
    • 57

    Mysql Query grouping and having clause

    Dear Sir

    Please help me
    i want a query which out put is:

    Code:
    Vendor                                Amount
    Computer
    AA                                       52000
    BB                                       20000
    Total:                                   72000
    Printer
    XX                                       12000
    YY                                      18000
    Total:                                  30000
    Thanks
    Last edited by Atli; Dec 15 '09, 09:52 AM. Reason: Added code tags.
  • mwasif
    Recognized Expert Contributor
    • Jul 2006
    • 802

    #2
    How your database look like?

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      Hey.

      MySQL returns a two-dimensional list of data. What you posted is three-dimensional. You should generally avoid making MySQL jump through hoops to format the data in such a way, but rather leave that to your front-end code.

      Having said that, check out the WITH ROLLUP modifier for the GROUP BY clause. It can give you a 2D representation of your 3D results. Not exactly what you posted, but close enough.

      Comment

      • Noorain
        New Member
        • Mar 2008
        • 57

        #4
        I used 3 tables for this query. Please help me


        Code:
        CREATE TABLE `item` (
          `id` int(11) NOT NULL auto_increment,
          `item_name` varchar(3) collate latin1_general_ci NOT NULL,
        PRIMARY KEY  (`id`)
        ) 
        
        INSERT INTO `item` VALUES (1, 'Computer');
        INSERT INTO `item` VALUES (2, 'Printer');
        Code:
        CREATE TABLE `vndr` (
          `id` int(11) NOT NULL auto_increment,
          `vndr_name` varchar(255) collate latin1_general_ci NOT NULL,
          PRIMARY KEY  (`id`)
        ) 
        
        INSERT INTO `vndr` VALUES (1, 'AA');
        INSERT INTO `vndr` VALUES (2, 'BB');
        INSERT INTO `vndr` VALUES (3, 'EE');
        INSERT INTO `vndr` VALUES (4, RR');
        INSERT INTO `vndr` VALUES (5, 'WW');
        Code:
        CREATE TABLE `vt` (
          `id` int(11) NOT NULL auto_increment,
          `item_id` int(11) NOT NULL,
          `vndr_id` int(11) NOT NULL,
          `amount` double(10,2) NOT NULL,
          `remarks` varchar(255) collate latin1_general_ci NOT NULL,
        PRIMARY KEY  (`id`)
        )
        
        INSERT INTO `vt` VALUES (1, 1, 1,  25000.00, '');
        INSERT INTO `vt` VALUES (2, 1,1,25000.00, '');
        INSERT INTO `vt` VALUES ( 3, 1, 2, 25000.00, '');
        INSERT INTO `vt` VALUES (4, 2,4,  12000.00, '');
        INSERT INTO `vt` VALUES (5, 2, 3,  18000.00, '');
        INSERT INTO `vt` VALUES (6, 2, 3,  19000.00, '');
        Thanks
        Last edited by Atli; Dec 15 '09, 10:13 AM. Reason: Added code tags.

        Comment

        Working...