1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Is there a better way to run this query?

Discussion in 'MySQL' started by imvain2, Feb 28, 2010.

  1. #1
    I'm trying to find values that aren't being used in one table, but are located in another table.

    My below query works, but I don't know if it is the optimal solution. I would also like to use only one query.

    select distinct(article_cats.category_id) from articles, article_cats
    where article_cats.category_id NOT IN (select category_id from articles)
    Code (markup):
    sample tables:

    articles:
    article_id
    title
    category_id

    article_cats:
    category_id
    category_name

    sample data:

    articles:
    article_id category_id
    1 2
    2 3
    3 1
    4 1

    article_cats:

    category_id category_name
    1 A
    2 B
    3 C
    4 E

    sample query results would just return the value of 4
     
    imvain2, Feb 28, 2010 IP
  2. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #2
    i think its the optimal.. try to look for its loading time,
    based on my experience it more slower if you use left join..

    recent experience
    table with more than 100k rows
    with left join 10 secs - 15 secs, then i revised it like your query its just .5 - 3 secs
     
    bartolay13, Feb 28, 2010 IP
    imvain2 likes this.
  3. hireme

    hireme Member

    Messages:
    58
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    45
    #3
    why not do that query into this:

    
    select distinct(category_id) from article_cats
    where category_id NOT IN (select category_id from articles)
    
    Code (markup):
     
    hireme, Feb 28, 2010 IP
    imvain2 likes this.
  4. imvain2

    imvain2 Peon

    Messages:
    218
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #4
    barto:
    thanks for the information. It's funny, I have been programming and working with mysql for a long time now and just occurred to me that I was able to do this all in one query. I always think of passing IN, a simple list value and never it's own seperate query.

    hireme:
    That is a good question. Since, I'm no longer connecting to two tables with where and and statements, I don't actually need to reference the articles table.

    Thanks everyone.
     
    imvain2, Feb 28, 2010 IP
  5. blacksheep666

    blacksheep666 Active Member

    Messages:
    68
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    93
    #5
    try this

    "select articles.category_id from articles inner join articles.category_id = article_cats.category_id"

    use inner join.. if table a does not match with table b it wont return a record.
     
    blacksheep666, Mar 4, 2010 IP