Selecting data from two different tables in phpmyadmin

Discussion in 'Programming' started by globalcashsite, Jun 29, 2009.

  1. #1
    Guys

    I have two tables,

    A = Client table (client_id, client_name fields)
    B = History table (amount field)

    I want to select all clients from Client tables who have any record/amount in History Table.

    Please let me know how to do this?

    Thank you


    GCS
     
    globalcashsite, Jun 29, 2009 IP
  2. FDIM

    FDIM Member

    Messages:
    55
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #2
    try this
    
    SELECT client_id as client,client_name,(SELECT count(id) FROM history_table WHERE client_id=client) AS entries FROM clients_table WHERE entries>0
    
    Code (markup):
     
    FDIM, Jun 29, 2009 IP
  3. globalcashsite

    globalcashsite Peon

    Messages:
    806
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi buddy,

    Thanks for helping but it gives error

    Documentation #1054 - Unknown column 'entries' in 'where clause'

    I have another question you used "WHERE client_id=client" in this case I will use client_id=client_id because in both tables there is same field client_id. How SQL will know that with which client_id to match?

    GCS
     
    globalcashsite, Jun 29, 2009 IP
  4. Aces A

    Aces A Peon

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    The correct forum for this question is the databases forum (http://forums.digitalpoint.com/forumdisplay.php?f=57).

    If client_id is a field in both tables, you can refer to it in your query by doing table_A_name.client_id or table_B_name.client_id (where table_A_name is the name of your A table).

    You can try this query:
    SELECT DISTINCT table_A.client_id, client_name FROM table_A WHERE table_A.client_id = table_B.client_id
     
    Aces A, Jun 29, 2009 IP
  5. globalcashsite

    globalcashsite Peon

    Messages:
    806
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thank you for your help but your command is not complete. That is missing GROUP BY option and also after DIST you did not selected field from table_B.

    Anyhow you used DISTINCT and that clicked me and after some research I was able to do what I was trying to do. :D

    GCS
     
    globalcashsite, Jun 29, 2009 IP
  6. FDIM

    FDIM Member

    Messages:
    55
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #6
    i havent tested the query i wrote
    
    SELECT client_id as client,client_name FROM clients_table WHERE (SELECT count(id) FROM history_table WHERE client_id=client)>0
    
    Code (markup):
    this should work
    You can rename these like i did in here
    
    SELECT client_id as client .....
    
    Code (markup):
    after this when u need client_id column just type client
     
    FDIM, Jun 30, 2009 IP
  7. mioot

    mioot Peon

    Messages:
    169
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Try this

    SELECT client.name,history.amount FROM HISTORY LEFT JOIN client ON client .client_id = history.client_id
     
    mioot, Jul 1, 2009 IP