Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Results 1 to 3 of 3

Thread: [help] Query from two tables?

  1. #1
    77
    posts
    Registered User

    [help] Query from two tables?

    I'm wondering how to go about this? I'm not sure if its even possible or if I need to use a join or something?

    Im using a search facility on my page. When a user types something into the search box I need it to search inside two separate tables. Can this be done?

    The results for the query will be run inside a while statement.

    For the one table I will need to find users names and the other one picture titles.

    I would really appreciate any help?

  2. #2

    Dot syntax for multiple tables

    when you run your query, there is a dot-joined syntax for querying multiple tables:

    The basic structure is this: columnName.tableName

    some examples:
    SELECT username.usertable, pics.pictable FROM usertable, pictable...
    (username = the name of your user name column, and usertable = the name of the table that holds the usernames, pics = the name of the picture title column, pictable = the name of the table holding the picture titles. )

    You can also assign the keyword 'AS' to create an alias for the rows:

    SELECT username.usertable AS user, pics.pictable AS picTitles FROM...

    You can even compare columns from one table to columns of another:

    ...WHERE username.usertable = picowner.pictable...

    Quote Originally Posted by cjj View Post
    I'm wondering how to go about this? I'm not sure if its even possible or if I need to use a join or something?

    Im using a search facility on my page. When a user types something into the search box I need it to search inside two separate tables. Can this be done?

    The results for the query will be run inside a while statement.

    For the one table I will need to find users names and the other one picture titles.

    I would really appreciate any help?

  3. #3
    77
    posts
    Registered User
    I'm not sure how this would work, as I have a WHERE in my query? Let me show you what the two separate queries would look like and maybe you can show me how to put them together. I would really appreciate it!!

    $query1 = "SELECT * FROM items WHERE (item_title LIKE '% $searching %') order by
    item_title asc ";

    $query2 = "SELECT username FROM users WHERE (username LIKE '% $searching %') order
    by username asc";

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
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012