www.webdeveloper.com
Results 1 to 3 of 3

Thread: Cannot reach id from table when using SELECT JOIN

  1. #1
    Join Date
    May 2014
    Posts
    9

    Cannot reach id from table when using SELECT JOIN

    Hello, I have a problem to select the id from the filmer-table when I use JOIN in the SQL-string below. At the moment I only reach the id from the regissorer-table, but I want to reach both id:s from both tables. The thing is that I want to point to a specific movie using its id from the filmer-table in a link, but I also need to be able to point to the regissorer_id to be able to print out names of directors in text.

    So, does someone know if this could be fixed in the same SQL-string, like adding some SQL-code to it so I can select both id:s? Would be thankful if someone could help me with this.

    My code:
    PHP Code:
    $sql "SELECT * FROM filmer JOIN regissorer ON regissorer.id = filmer.regissor_id"

  2. #2
    Join Date
    Dec 2002
    Location
    Seattle, WA
    Posts
    1,843
    You cannot use the SELECT * (select all) with a JOIN. you can however do a "selective" select all.
    Code:
    SELECT f.*, r.col1, ..., r.colN
      FROM filmer 'f'
        JOIN regissorer 'r'
          ON r.id = f.regissor_id
    Replace r.col1, ..., r.colN with the columns you want from regissorer.
    The filmer 'f' (or filmer AS 'f') is an alias (simply there for illustration, but comes in handy on very long statements).

  3. #3
    Join Date
    May 2014
    Posts
    9
    Quote Originally Posted by ShrineDesigns View Post
    You cannot use the SELECT * (select all) with a JOIN. you can however do a "selective" select all.
    Code:
    SELECT f.*, r.col1, ..., r.colN
      FROM filmer 'f'
        JOIN regissorer 'r'
          ON r.id = f.regissor_id
    Replace r.col1, ..., r.colN with the columns you want from regissorer.
    The filmer 'f' (or filmer AS 'f') is an alias (simply there for illustration, but comes in handy on very long statements).
    That solved the problem, thank you very much! :-D

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles