SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: SELECT from Multi* table

  1. #1
    SitePoint Evangelist
    Join Date
    Apr 2005
    Location
    London, UK
    Posts
    497
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    SELECT from Multi* table

    Folks,

    How would I do a select that searches multiple tables, but instead of listing all tables I want to do something like: WHERE TABLE_NAME LIKE 'a%'

    I basically want something like this:

    SELECT email FROM TABLE_NAME LIKE 'a%' WHERE email ='foo@doo.com';

    Thanks

  2. #2
    SQL Consultant silver trophybronze trophy
    SitePoint Award Recipient r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    38,468
    Mentioned
    35 Post(s)
    Tagged
    1 Thread(s)
    you can't do it that way, you'll have to search each table separately (maybe do them all in a big UNION query)

    why do you have multiple tables for emails?
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    #titanic {float:none} silver trophy
    molona's Avatar
    Join Date
    Feb 2005
    Location
    from Madrid to Heaven
    Posts
    7,346
    Mentioned
    116 Post(s)
    Tagged
    1 Thread(s)
    As far as I know, you can use matching patterns if you use the shell of myslq but I have never tried to do a select query where I needed to use a matching pattern.

    I do know that in Access and SQL Server you can do something like
    Code:
    select * from sys.tables where name like '%table%'
    so if you're using any of these, then the answer will be yes.

    On any other database, I would say that there has to be a way but right now I wouldn't be able to know how.
    Before asking, do a search... if you don't find the answer, then ask
    The purpose of this forum is to help others in the community, that's why it's called Sitepoint and not Linkpoint.
    SP Guidelines - No fluff.

    Thinking Web: Voices of the Community - The Community Book

  4. #4
    SitePoint Evangelist
    Join Date
    Apr 2005
    Location
    London, UK
    Posts
    497
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Rudy. I was battling with it for a while, but yeah UNION sprung into my mind too. Why multiple tables for email? Well that’s the one million dollar question, I inherited it all.

  5. #5
    SitePoint Evangelist
    Join Date
    Apr 2005
    Location
    London, UK
    Posts
    497
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    MySQL allows something similar:

    SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME LIKE 'a%';

    But further querying the result set of all tables in the above query is the tricky or not straight forward part without having to do as r937 suggested.

  6. #6
    SQL Consultant silver trophybronze trophy
    SitePoint Award Recipient r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    38,468
    Mentioned
    35 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by molona View Post
    I do know that in Access and SQL Server you can do something like...
    in most databases, this is the INFORMATION_SCHEMA

    Edit: darn, sniped by five minutes... i'm getting slow

    it would require two queries, one for the metadata, and then one or more data queries (one if you use the UNION approach)

    SQL Server and MySQL both support INFORMATION_SCHEMA, which i believe is part of the SQL standard
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  7. #7
    #titanic {float:none} silver trophy
    molona's Avatar
    Join Date
    Feb 2005
    Location
    from Madrid to Heaven
    Posts
    7,346
    Mentioned
    116 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by r937 View Post
    in most databases, this is the INFORMATION_SCHEMA

    Edit: darn, sniped by five minutes... i'm getting slow

    it would require two queries, one for the metadata, and then one or more data queries (one if you use the UNION approach)

    SQL Server and MySQL both support INFORMATION_SCHEMA, which i believe is part of the SQL standard
    I thought that there had to be something similar... I'm glad that I was right Thanks.

    I didn't suggest the union approach because I assumed that ma201dq wanted to query for tables with similar name first.
    Before asking, do a search... if you don't find the answer, then ask
    The purpose of this forum is to help others in the community, that's why it's called Sitepoint and not Linkpoint.
    SP Guidelines - No fluff.

    Thinking Web: Voices of the Community - The Community Book

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
  •