Results 1 to 3 of 3

Thread: [SQL] Getting client names from other table knowing only theirs ids

  1. #1
    Private First Class RobsoN's Avatar
    Join Date
    Jan 2013
    Location
    /home/cod2/
    Posts
    230
    Thanks
    119
    Thanked 95 Times in 64 Posts

    Cool [SQL] Getting client names from other table knowing only theirs ids

    Hello, Im new in SQL. I want to get in one query: reason, banned NAME, admin NAME (thats missing in my query - i need this :P), ban type, time_add, time_expire, type. My current query looks like this:

    PHP Code:
    SELECT p.idp.typep.client_idp.time_expirep.time_addp.reason AS ban_reasonc.name AS banned_clientp.admin_id AS banned_by FROM penalties pclients c WHERE (p.inactive '0' AND (p.time_expire UNIX_TIMESTAMP() OR p.time_expire 1)) AND (p.type 'TempBan' OR p.type 'Ban') AND c.id p.client_id 
    As you can see, I got 2 tables.

    1st table "clients" with id, name (...)
    2nd table "penalties" with client_id, admin_id, time_add, reason, type

    I dont know how can I get names (admin name + banned name) in one query, knowning only clients ids ..

    Thanks in advice.
    Regards RobsoN.
    "Don't worry if your code doesn't work correctly - if everything worked, you would not work" ~Mosher's right

  2. #2
    Assadministrator IzNoGoD's Avatar
    Join Date
    Aug 2012
    Posts
    1,718
    Thanks
    17
    Thanked 1,068 Times in 674 Posts
    Code:
    SELECT p.name, a.name FROM (SELECT client_id, admin_id FROM penalties WHERE ...[your where clause goes here if any]) b INNER JOIN (SELECT name, id FROM clients) a ON (a.id = b.admin_id) INNER JOIN (SELECT name, id FROM clients) p ON b.client_id = p.id
    "Does not work" is an error report for a bug between keyboard and chair.

    All hail Artie Effem

  3. The Following User Says Thank You to IzNoGoD For This Useful Post:

    RobsoN (23rd March 2014)

  4. #3
    Private First Class RobsoN's Avatar
    Join Date
    Jan 2013
    Location
    /home/cod2/
    Posts
    230
    Thanks
    119
    Thanked 95 Times in 64 Posts
    Thanks a lot!
    "Don't worry if your code doesn't work correctly - if everything worked, you would not work" ~Mosher's right

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •