Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. MySQL
  4. combining 2 or more sql statements

combining 2 or more sql statements

Scheduled Pinned Locked Moved MySQL
databasesaleshelp
5 Posts 5 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    abushahin
    wrote on last edited by
    #1

    hey there! ive got four queries that need combining 1:

    SELECT amount, notes FROM credit WHERE customer_Id = $cust_id

    2:

    SELECT amount, notes FROM payment WHERE customer_Id = $cust_id

    3:

    SELECT sum( amount ) AS totalpaid FROM payment WHERE customer_Id = $cust_id

    4:

    SELECT sum( amount ) AS totaltaken FROM credit WHERE customer_Id = $cust_id

    im not totally fluent at sql queries but these are the queries that id like to be able to retreive all at once via ine single query instead of the repetition four times, any help appreciated thanks abu ;P

    http://abushahin.eu

    L S 2 Replies Last reply
    0
    • A abushahin

      hey there! ive got four queries that need combining 1:

      SELECT amount, notes FROM credit WHERE customer_Id = $cust_id

      2:

      SELECT amount, notes FROM payment WHERE customer_Id = $cust_id

      3:

      SELECT sum( amount ) AS totalpaid FROM payment WHERE customer_Id = $cust_id

      4:

      SELECT sum( amount ) AS totaltaken FROM credit WHERE customer_Id = $cust_id

      im not totally fluent at sql queries but these are the queries that id like to be able to retreive all at once via ine single query instead of the repetition four times, any help appreciated thanks abu ;P

      http://abushahin.eu

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Try this:

      SELECT amount, notes FROM credit WHERE customer_Id = $cust_id
      UNION ALL
      SELECT amount, notes FROM payment WHERE customer_Id = $cust_id
      UNION ALL
      SELECT sum( amount ), '' AS totalpaid FROM payment WHERE customer_Id = $cust_id
      UNION ALL
      SELECT sum( amount ), '' AS totaltaken FROM credit WHERE customer_Id = $cust_id

      M 1 Reply Last reply
      0
      • A abushahin

        hey there! ive got four queries that need combining 1:

        SELECT amount, notes FROM credit WHERE customer_Id = $cust_id

        2:

        SELECT amount, notes FROM payment WHERE customer_Id = $cust_id

        3:

        SELECT sum( amount ) AS totalpaid FROM payment WHERE customer_Id = $cust_id

        4:

        SELECT sum( amount ) AS totaltaken FROM credit WHERE customer_Id = $cust_id

        im not totally fluent at sql queries but these are the queries that id like to be able to retreive all at once via ine single query instead of the repetition four times, any help appreciated thanks abu ;P

        http://abushahin.eu

        S Offline
        S Offline
        suguimoto
        wrote on last edited by
        #3

        1, 2: Try this:

        SELECT amount, note FROM credit, payment WHERE customer_Id = $cust_id

        To join them all, try JOIN command, I really dont remember how it would work for you.

        1 Reply Last reply
        0
        • L Lost User

          Try this:

          SELECT amount, notes FROM credit WHERE customer_Id = $cust_id
          UNION ALL
          SELECT amount, notes FROM payment WHERE customer_Id = $cust_id
          UNION ALL
          SELECT sum( amount ), '' AS totalpaid FROM payment WHERE customer_Id = $cust_id
          UNION ALL
          SELECT sum( amount ), '' AS totaltaken FROM credit WHERE customer_Id = $cust_id

          M Offline
          M Offline
          miss YY
          wrote on last edited by
          #4

          when you do this,it only union those fields,can not make sum(amount) show as an Independent field.how could we do?

          A 1 Reply Last reply
          0
          • M miss YY

            when you do this,it only union those fields,can not make sum(amount) show as an Independent field.how could we do?

            A Offline
            A Offline
            Arun Immanuel
            wrote on last edited by
            #5

            SELECT SUM(A.amount) FROM (
            SELECT amount, notes FROM credit WHERE customer_Id = $cust_id
            UNION ALL
            SELECT amount, notes FROM payment WHERE customer_Id = $cust_id
            UNION ALL
            SELECT sum( amount ), '' AS totalpaid FROM payment WHERE customer_Id = $cust_id
            UNION ALL
            SELECT sum( amount ), '' AS totaltaken FROM credit WHERE customer_Id = $cust_id
            ) A

            ;

            Regards, Arun Kumar.A

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups