Thursday, May 5, 2011

Question about joining two mysql tables.

I am trying to join two tables; the purpose being able to search and display event information for an artist that is entered by the user.

The tables are as follows:

artist table: [id],[name]

events table: [id],[artist_id],[venue_name],[city],[state],[date],[time]

I created a search engine, but what I want to do is when an artist name is entered into the text box, the code will go out to my database and will look through the artist table to see what name was entered and then grab the id that matches the entered name and then go to the events table to find that distinct artist_id and display all of the event information for the certain artist that was entered.

I would really like help with how to do this and I'm not the greatest at this, but I am trying! Thanks:)

From stackoverflow
  • select e.venue_name, e.city, e.state, e.date, e.time
    from artist_table a
      join events_table e
        on a.id = e.artist_id
    where a.name = @userInput
    

    or something like that... Or am I missing something? Is this a homework question?

    duffymo : +1 from me - the point goes to the swift. 8)
    Holly : yes, it's a homework project due tomorrow :/
  • SELECT *
    FROM artist
        LEFT JOIN events
            ON artist.id = events.artist_id
    WHERE artist.name = 'your search text'
    
    Adrien : Not to stump for my own answer ... But presuming a fairly normalized database, this is going to return more data than is strictly necessary. Still, +1 for being structurally sound. Also, watch for input validation on the 'your search text' bit. SQL Injection waiting to happen there.
    LukeH : @Adrien, You're probably right, but I assumed that they'd also want the artist details for display in the UI or whatever, hence the "SELECT *" and the "LEFT JOIN".
    Adrien : @Luke: Agreed, hence the upvote from me. I've been accused of "overanalyzing" the problem (especially homework problems) from time to time. :)
  • select a.name, e.* 
    from artist as a
        left join events on(a.id - e.artist_id)
            where a.name like '%$someinput%'

    just in case you dont want to find name exact match.

    Just trying to help. and its sure i'm not great at this ,but want to help since at first it's complicated for me too

0 comments:

Post a Comment