SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Nov 2009
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    search query help!!

    hi m new to this forums..hope my problem will be solved here..

    I have three tables menu_content, services and tailor..
    all 3 tables are not linked..
    their desc is as follows
    menu_content has menu_id, title and description
    tailor has tailid,title and content
    services has servid,title and content

    i want to write a search query tht searches in all 3 tables for a keyword and displays result..

    plz help me.

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,496
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Welcome to SP

    Use UNION

  3. #3
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,201
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In more specific form:

    Code MySQL:
    SELECT
    	*
    FROM
    	menu_content
    WHERE 
    	title LIKE '%keyword goes here%' 
    	OR description LIKE '%keyword goes here%'
     
    UNION
     
    SELECT
    	*
    FROM
    	tailor
    WHERE 
    	title LIKE '%keyword goes here%' 
    	OR content LIKE '%keyword goes here%'
     
    UNION
     
    SELECT
    	*
    FROM
    	services
    WHERE 
    	title LIKE '%keyword goes here%' 
    	OR content LIKE '%keyword goes here%'

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,251
    Mentioned
    59 Post(s)
    Tagged
    3 Thread(s)
    sorry, phpycho, your use of the dreaded, evil "select star" will almost certainly cause the query to fail in this case

    i know he said each table has exactly three columns, but i don't believe it!!!

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,201
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937 View Post
    sorry, phpycho, your use of the dreaded, evil "select star" will almost certainly cause the query to fail in this case

    i know he said each table has exactly three columns, but i don't believe it!!!

    Ya sure. column types must be matched in Case of UNION.

    I must have typed the fields to be fetched (without being lazy)

  6. #6
    SitePoint Member
    Join Date
    Nov 2009
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hey guys thanks a lot..but my prob is solved!
    I used three separate queries n den displayed the result in for loops..

  7. #7
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,496
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by new_me View Post
    hey guys thanks a lot..but my prob is solved!
    I used three separate queries n den displayed the result in for loops..
    Great.
    But you might want to look into UNION's non the less. First, because you'd learn something new
    Second, because it would mean only one DB access instead of three, which means less resource usage and faster results.

  8. #8
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,014
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    new_me can you show a "Create Table" for each of the three tables? Which fields do you need in the results? The use of SELECT * is generally considered to be bad practise, you should only select the columns that you need.
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator


Bookmarks

Posting Permissions

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