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. Database
  4. selecting data from two tables

selecting data from two tables

Scheduled Pinned Locked Moved Database
databaseagentic-aisales
9 Posts 5 Posters 0 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.
  • M Offline
    M Offline
    Member 10263519
    wrote on last edited by
    #1

    hi, I need to select data from two tables am using the following query:

    select c.customer_id ,c.father_name ,c.address,c.phone_number ,c.mobile_number ,c.id_proof ,c.area ,c.ip_address as 'IP Address',c.mac_address as 'MAC Address',c.package_type as 'Package Type',c.name as Name,c.activation_date as 'Activation Date',c.status as 'Status',c.installation_cost as 'Installation Cost',c.totalamount_paid as 'Total Amount Paid',c.monthly_amount as 'Monthly Amount',c.lastpaid_date as 'Last Paid Date',c.lastpaid_amount as 'Last Paid Amount',c.nextpay_date as 'Next Pay Date',c.totaldue_amount as 'Total Due Amount',t.agent_id as 'Agent ID',t.token_number as 'Token Number',t.machine_id as 'Machine ID' from customer c,transaction t where c.customer_id=t.customer_id and DATE(t.paid_date)BETWEEN '2014-02-02'AND '2014-04-02' ;

    It's giving correct information but each record is displayed twice , i want a record only once.

    Richard DeemingR J B B 4 Replies Last reply
    0
    • M Member 10263519

      hi, I need to select data from two tables am using the following query:

      select c.customer_id ,c.father_name ,c.address,c.phone_number ,c.mobile_number ,c.id_proof ,c.area ,c.ip_address as 'IP Address',c.mac_address as 'MAC Address',c.package_type as 'Package Type',c.name as Name,c.activation_date as 'Activation Date',c.status as 'Status',c.installation_cost as 'Installation Cost',c.totalamount_paid as 'Total Amount Paid',c.monthly_amount as 'Monthly Amount',c.lastpaid_date as 'Last Paid Date',c.lastpaid_amount as 'Last Paid Amount',c.nextpay_date as 'Next Pay Date',c.totaldue_amount as 'Total Due Amount',t.agent_id as 'Agent ID',t.token_number as 'Token Number',t.machine_id as 'Machine ID' from customer c,transaction t where c.customer_id=t.customer_id and DATE(t.paid_date)BETWEEN '2014-02-02'AND '2014-04-02' ;

      It's giving correct information but each record is displayed twice , i want a record only once.

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      You have two matching rows in the transaction table for each customer, and you've asked SQL to return all of them, so it's hardly surprising that you get two rows back per customer. If you only want the most recent transaction for each customer, there are ways to do that. However, it would help if you told us which DBMS you're using. Also, try to avoid using old-style joins:

      ... FROM customer c,transaction t WHERE c.customer_id = t.customer_id And ...

      Use an INNER JOIN instead:

      ... FROM customer c INNER JOIN transaction t ON c.customer_id = t.customer_id WHERE ...

      It makes it much easier to separate the joining conditions from the filtering conditions, and to switch between different types of join.


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      M 1 Reply Last reply
      0
      • M Member 10263519

        hi, I need to select data from two tables am using the following query:

        select c.customer_id ,c.father_name ,c.address,c.phone_number ,c.mobile_number ,c.id_proof ,c.area ,c.ip_address as 'IP Address',c.mac_address as 'MAC Address',c.package_type as 'Package Type',c.name as Name,c.activation_date as 'Activation Date',c.status as 'Status',c.installation_cost as 'Installation Cost',c.totalamount_paid as 'Total Amount Paid',c.monthly_amount as 'Monthly Amount',c.lastpaid_date as 'Last Paid Date',c.lastpaid_amount as 'Last Paid Amount',c.nextpay_date as 'Next Pay Date',c.totaldue_amount as 'Total Due Amount',t.agent_id as 'Agent ID',t.token_number as 'Token Number',t.machine_id as 'Machine ID' from customer c,transaction t where c.customer_id=t.customer_id and DATE(t.paid_date)BETWEEN '2014-02-02'AND '2014-04-02' ;

        It's giving correct information but each record is displayed twice , i want a record only once.

        J Offline
        J Offline
        Jorgen Andersson
        wrote on last edited by
        #3

        I guess it's safe to assume that it's monthly payments, and that BETWEEN '2014-02-02'AND '2014-04-02' you'd have two payments per customer_id.

        Wrong is evil and must be defeated. - Jeff Ello[^]

        1 Reply Last reply
        0
        • Richard DeemingR Richard Deeming

          You have two matching rows in the transaction table for each customer, and you've asked SQL to return all of them, so it's hardly surprising that you get two rows back per customer. If you only want the most recent transaction for each customer, there are ways to do that. However, it would help if you told us which DBMS you're using. Also, try to avoid using old-style joins:

          ... FROM customer c,transaction t WHERE c.customer_id = t.customer_id And ...

          Use an INNER JOIN instead:

          ... FROM customer c INNER JOIN transaction t ON c.customer_id = t.customer_id WHERE ...

          It makes it much easier to separate the joining conditions from the filtering conditions, and to switch between different types of join.


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          M Offline
          M Offline
          Member 10263519
          wrote on last edited by
          #4

          Mysql database we are using.yes my transaction table is having two records per customer id, but paid amounts and other fields are different , but here the query is giving last record values for two records having same customerid. Transaction table doesn't contain primary key,so there exists more than one record per customer id. and i want most recent record from Database.

          Richard DeemingR 1 Reply Last reply
          0
          • M Member 10263519

            hi, I need to select data from two tables am using the following query:

            select c.customer_id ,c.father_name ,c.address,c.phone_number ,c.mobile_number ,c.id_proof ,c.area ,c.ip_address as 'IP Address',c.mac_address as 'MAC Address',c.package_type as 'Package Type',c.name as Name,c.activation_date as 'Activation Date',c.status as 'Status',c.installation_cost as 'Installation Cost',c.totalamount_paid as 'Total Amount Paid',c.monthly_amount as 'Monthly Amount',c.lastpaid_date as 'Last Paid Date',c.lastpaid_amount as 'Last Paid Amount',c.nextpay_date as 'Next Pay Date',c.totaldue_amount as 'Total Due Amount',t.agent_id as 'Agent ID',t.token_number as 'Token Number',t.machine_id as 'Machine ID' from customer c,transaction t where c.customer_id=t.customer_id and DATE(t.paid_date)BETWEEN '2014-02-02'AND '2014-04-02' ;

            It's giving correct information but each record is displayed twice , i want a record only once.

            B Offline
            B Offline
            Bernhard Hiller
            wrote on last edited by
            #5

            Where do you display the result? In a .Net application written by you / your colleagues? In MySQL Query browser? ...?

            M 1 Reply Last reply
            0
            • B Bernhard Hiller

              Where do you display the result? In a .Net application written by you / your colleagues? In MySQL Query browser? ...?

              M Offline
              M Offline
              Member 10263519
              wrote on last edited by
              #6

              am generating EXcel/ PDF file,in .net application written by me,

              B 1 Reply Last reply
              0
              • M Member 10263519

                Mysql database we are using.yes my transaction table is having two records per customer id, but paid amounts and other fields are different , but here the query is giving last record values for two records having same customerid. Transaction table doesn't contain primary key,so there exists more than one record per customer id. and i want most recent record from Database.

                Richard DeemingR Offline
                Richard DeemingR Offline
                Richard Deeming
                wrote on last edited by
                #7

                I'm not overly familiar with MySql, but something like this should do the trick:

                SELECT
                ...
                FROM
                customer c
                INNER JOIN transaction t
                ON t.customer_id = c.customer_id
                INNER JOIN
                (
                SELECT customer_id, Max(paid_date) As LastPaid
                FROM transaction
                WHERE DATE(paid_date) BETWEEN '2014-02-02' AND '2014-04-02'
                GROUP BY customer_id
                ) t2
                ON t2.customer_id = t.customer_id
                And t2.LastPaid = t.paid_date
                ;


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                1 Reply Last reply
                0
                • M Member 10263519

                  am generating EXcel/ PDF file,in .net application written by me,

                  B Offline
                  B Offline
                  Bernhard Hiller
                  wrote on last edited by
                  #8

                  Consequently, the first thing to check is if the data returned from the query to your application are correct, and then where that duplication happens. That's a lot of debugging...

                  1 Reply Last reply
                  0
                  • M Member 10263519

                    hi, I need to select data from two tables am using the following query:

                    select c.customer_id ,c.father_name ,c.address,c.phone_number ,c.mobile_number ,c.id_proof ,c.area ,c.ip_address as 'IP Address',c.mac_address as 'MAC Address',c.package_type as 'Package Type',c.name as Name,c.activation_date as 'Activation Date',c.status as 'Status',c.installation_cost as 'Installation Cost',c.totalamount_paid as 'Total Amount Paid',c.monthly_amount as 'Monthly Amount',c.lastpaid_date as 'Last Paid Date',c.lastpaid_amount as 'Last Paid Amount',c.nextpay_date as 'Next Pay Date',c.totaldue_amount as 'Total Due Amount',t.agent_id as 'Agent ID',t.token_number as 'Token Number',t.machine_id as 'Machine ID' from customer c,transaction t where c.customer_id=t.customer_id and DATE(t.paid_date)BETWEEN '2014-02-02'AND '2014-04-02' ;

                    It's giving correct information but each record is displayed twice , i want a record only once.

                    B Offline
                    B Offline
                    BobWayne07
                    wrote on last edited by
                    #9

                    Quote:

                    passive income online

                    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