Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

SQL problem.

  • 21-11-2005 01:25PM
    #1
    Registered Users, Registered Users 2 Posts: 883 ✭✭✭


    ("SELECT nickstats.nick AS ns, seeder, ip, port, uploaded, downloaded, to_go, UNIX_TIMESTAMP(started) AS st, connectable, UNIX_TIMESTAMP(last_action) AS la FROM (torrent_peers INNER JOIN nickstats_login ON torrent_peers.ip = nickstats_login.ip) INNER JOIN nickstats ON nickstats_login.nickstats_id = nickstats.id WHERE torrent = '$id'")

    I'm having problems running this query which links 3 separate tables. I'm using it in a .php page. Can anyone see any probs with it?

    I'm getting the following error:

    Error executing SQL Query SELECT nickstats.nick AS ns, seeder, ip,
    port, uploaded, downloaded, to_go, UNIX_TIMESTAMP(started) AS st,
    connectable, UNIX_TIMESTAMP(last_action) AS la FROM (torrent_peers
    INNER JOIN nickstats_login ON torrent_peers.ip = nickstats_login.ip)
    INNER JOIN nickstats ON nickstats_login.nickstats_id = nickstats.id
    WHERE torrent = '5192'
    Error ID: 1052
    Error Message: Column 'ip' in field list is ambiguous


Comments

  • Registered Users, Registered Users 2 Posts: 197 ✭✭drag0n79


    ("SELECT nickstats.nick AS ns, seeder, nickstats_login.ip, port, uploaded, downloaded, to_go, UNIX_TIMESTAMP(started) AS st, connectable, UNIX_TIMESTAMP(last_action) AS la FROM (torrent_peers INNER JOIN nickstats_login ON torrent_peers.ip = nickstats_login.ip) INNER JOIN nickstats ON nickstats_login.nickstats_id = nickstats.id WHERE torrent = '$id'")

    The IP field appears in (at least) 2 of your tables, so you've gotta explicitly say which one you want to SELECT.


  • Closed Accounts Posts: 3,357 ✭✭✭Beano


    Your query starts with

    SELECT nickstats.nick AS ns, seeder, ip

    you need to qualify the columns in the select list with the table alias if the same column exists in more than one of the tables selected from. Even if the column exists in only one of the tables it is still good practice.


  • Closed Accounts Posts: 3,357 ✭✭✭Beano


    damn, beaten to it.


  • Closed Accounts Posts: 756 ✭✭✭Zaph0d


    The SQL compiler doesn't know which table to use and it wants you to be more specific. Change ip in the first line to torrent_peers.ip or nickstats_login.ip depending on which one you want.


  • Registered Users, Registered Users 2 Posts: 883 ✭✭✭sirpsycho


    Damn fast replies :) Thanks, i'll fix it up and let you know how I get on.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 68,173 ✭✭✭✭seamus


    As it says, the 'ip' field in the select query is ambiguous because it exists in more than one table or subquery.
    Try
    ("SELECT nickstats.nick AS ns, sq.seeder, sq.ip, sq.port, sq.uploaded, sq.downloaded, sq.to_go, UNIX_TIMESTAMP(sq.started) AS st, sq.connectable, UNIX_TIMESTAMP(sq.last_action) AS la FROM (torrent_peers INNER JOIN nickstats_login ON torrent_peers.ip = nickstats_login.ip) sq INNER JOIN nickstats ON nickstats_login.nickstats_id = nickstats.id WHERE torrent = '$id'")
    
    This sets up an alias for the table/query in the FROM area, and tells the SELECT to explicitly choose the columns from that alias.

    It may need a bit of tweaking before it works.

    [Edit: LOL, well beaten to it]


Advertisement