Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: match value against value in other table

  1. #1
    Join Date
    Apr 2006
    Location
    Amsterdam
    Posts
    350
    Thanks
    20
    Thanked 11 Times in 11 Posts

    Default match value against value in other table

    Hi Guys,

    I am struggleing with the following. I am haveing a kind of a lotery, people recieved a unique code that they need to submit on a website. Unforunatly I recieved the list with unique codes after the lotery started from the printer/publisher. So I couldn't check if the entered code actually exist from the start. That what I want do now.

    I have two tables. One table with all the participants and the code they submited. And one thable with just all existing codes.

    Code:
    Table 1
    +--------+---------+--------+--------+
    | ID     |   name  |  code  | valid  |
    +--------+---------+--------+--------+
    | 10000  | John    | H3Jk4t |  true  |
    | 10001  | Eric    | A8JBsr |  true  |
    | 10002  | Sander  | H1HFzd |  false |
    | 10003  | Will    | P7LS3w |  true  |
    | 10004  | Denis   | Q4PF4q |  true  |
    | 10005  | Pete    | H9BQ5d |  false |
    | 10006  | Tom     | W3NG7g |  true  |
    | 10007  | Valarie | N5EF4l |  true  |
    | 10008  | Saskia  | S2BN8f |  true  |
    | 10009  | Patrick | C9KE2v |  true  |
    +--------+---------+--------+--------+
    
    Table 2
     
    +--------+
    |  code  |
    +--------+
    | H3Jk4t |
    | A8JBsr |
    | H1HFzd |
    | P7LS3w |
    | Q4PF4q |
    | H9BQ5d |
    | W3NG7g |
    | N5EF4l |
    | S2BN8f |
    | C9KE2v |
    +--------+
    I want the check if the codes in table 1 exist in table 2, if they do not exist I want the value in colm valid to be false.

    Can I do this with a LEFT JOIN in a update statement?

  2. #2
    Join Date
    Mar 2005
    Location
    Hampshire
    Posts
    432
    Thanks
    4
    Thanked 3 Times in 3 Posts

    Default

    Depending on how many codes you are talking about, and if it is going to be a one off I would do this

    Code:
    UPDATE Table1 t1
    SET valid = EXISTS(SELECT t2.code FROM Table1 t2 WHERE t2.code = t1.code)
    Note that I have not checked this, however, it should give you an idea

    Just chill

  3. #3
    Join Date
    Jun 2005
    Location
    London
    Posts
    24
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default

    How about a sub query - something like this might work

    UPDATE Table1
    set valid = true
    WHERE id IN
    (SELECT CODE from Table2)

    I havent tested either so please do a backup first !

  4. #4
    Join Date
    Apr 2006
    Location
    Amsterdam
    Posts
    350
    Thanks
    20
    Thanked 11 Times in 11 Posts

    Default

    Thnx guys,

    I am trying both query's, but they take an awful lot of time to process. I am already waiting 20 minuts on the result using MySQL query browser.

    There are over 15.000 records in table 1 and 200.000 records in table 2. So I imagine it will take a while.

  5. #5
    Join Date
    Feb 2004
    Posts
    4,877
    Thanks
    2
    Thanked 134 Times in 113 Posts

    Default

    Are you working on ALUMINIUM by any chance? ALUMINIUM started to timeout due to excessive load at about that time...

    In any case, no query of this type should take that long - the query is either wrong or the tables need indexes to support the query.
    Warren Ashcroft
    Red Fox UK Limited - Pioneers in Internet Technology
    http://www.redfoxuk.com
    w.ashcroft [at] redfoxuk.com

    NOTE: Forum Private Messaging should not be used to contact staff with support queries.

  6. #6
    Join Date
    Apr 2006
    Location
    Amsterdam
    Posts
    350
    Thanks
    20
    Thanked 11 Times in 11 Posts

    Default

    Yeah, that was me on ALUMINIUM . I noticed ALUMINIUM started to struggle so I moved tesing to my desktop. (Should have done so in the first place).

    I am not really sure, but isn't indexing only useful for SELECT statments? I thought it doesnt provide any benefits for UPDATE querys. Please correct me if I am wrong though.

    I am getting close. The two flowing querys work in showing all the records where the code matches in the two tables. But they both have different output, I cant discover what the differences is

    Code:
    SELECT `code` FROM `table1` WHERE EXISTS(SELECT `code` FROM `table2`)
    Code:
    SELECT * FROM `table1` A, `table2` B
    WHERE B.`code` =  A.`code`
    So I am searching for a query that only shows the records in table1 that contain codes who do not match in table2.

    I thuoght that would be easy, just change WHERE EXISTS to WHERE NOT EXISTS or WHERE B.`code` = A.`code` in to WHERE B.`code` != A.`code`. But that doenst seem to work.

    Any one any pointers?

  7. #7
    Join Date
    Feb 2004
    Posts
    4,877
    Thanks
    2
    Thanked 134 Times in 113 Posts

    Default

    Quote Originally Posted by Rappie View Post
    I am not really sure, but isn't indexing only useful for SELECT statments?
    Not necessarily, they will assist any query that has a WHERE clause or a JOIN on columns .
    Warren Ashcroft
    Red Fox UK Limited - Pioneers in Internet Technology
    http://www.redfoxuk.com
    w.ashcroft [at] redfoxuk.com

    NOTE: Forum Private Messaging should not be used to contact staff with support queries.

  8. #8
    Join Date
    Jan 2006
    Posts
    419
    Thanks
    2
    Thanked 16 Times in 16 Posts

    Default

    Quote Originally Posted by webb0 View Post
    How about a sub query - something like this might work

    UPDATE Table1
    set valid = true
    WHERE id IN
    (SELECT CODE from Table2)

    I havent tested either so please do a backup first !
    The above is good approach and good advice regarding backup.

    Add indexes on both tables on the code columns.

  9. #9
    Join Date
    Apr 2006
    Location
    Amsterdam
    Posts
    350
    Thanks
    20
    Thanked 11 Times in 11 Posts

    Default

    Webb0's query really helped me out, I am using something similair.

    Code:
    UPDATE Table1 set valid = 'false'
    WHERE active = 'true' AND code NOT IN
    (SELECT code FROM Table2)
    Adding indexes really helped too. One more question though, should I index the `valid` colum as well? Because of the use in the WHERE clause.
    Last edited by Rappie; 1st November 2008 at 12:43 AM.

  10. #10
    Join Date
    Feb 2004
    Posts
    4,877
    Thanks
    2
    Thanked 134 Times in 113 Posts

    Default

    Quote Originally Posted by Rappie View Post
    Webb0's query really helped me out, I am using something similair.

    Code:
    UPDATE Table1 set valid = 'false'
    WHERE active = 'true' AND code NOT IN
    (SELECT code FROM Table2)
    Adding indexes really helped too. One more question though, should I index the `valid` colum as well? Because of the use in the WHERE clause.
    I don't see the valid column in the where clause?
    Warren Ashcroft
    Red Fox UK Limited - Pioneers in Internet Technology
    http://www.redfoxuk.com
    w.ashcroft [at] redfoxuk.com

    NOTE: Forum Private Messaging should not be used to contact staff with support queries.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. One query table
    By Rappie in forum MySQL
    Replies: 2
    Last Post: 17th March 2008, 11:03 PM
  2. Multiple Table Query
    By spire128 in forum MSSQL
    Replies: 6
    Last Post: 20th October 2006, 05:21 PM
  3. Embed Pivot Table
    By carlosgilf in forum ASP.NET
    Replies: 0
    Last Post: 8th September 2006, 04:47 PM
  4. Table-less design, css & XHTML
    By JohnnyW in forum Website and Graphic Design
    Replies: 2
    Last Post: 22nd December 2005, 01:06 PM
  5. Table Corner Rounding
    By Breaks in forum Development Support
    Replies: 8
    Last Post: 2nd November 2005, 10:21 PM

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
  •