Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: Subquery issue.

  1. #1
    Regular Coder
    Join Date
    Nov 2007
    Posts
    680
    Thanks
    319
    Thanked 1 Time in 1 Post

    Subquery issue.

    Query: sql = "SELECT * FROM images WHERE ID IN (SELECT jpgId FROM sales WHERE printed = 0)"

    I'm getting the following error.
    Code:
    Type mismatch in expression.
    Here is an image of my tables. Obviously I want to fetch row marked "1" using the jpgId value from each row that is returned in the sales table (labeled "2")

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,189
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    *PROBABLY* this means that jpgId in the sales table is not the same data type as id in the images table.

    If you had shown the SCHEMA of the two tables, instead of a (mostly useless) data dump I could tell you for sure.

    In any case, that's an inefficient way to do that.
    Code:
    SELECT I.* FROM images AS I, sales AS S WHERE I.id = S.jpgid AND S.printed = 0
    would be much better.

    But that will still give you a type mismatch if ID and jpgId are not the same datatype.

    I would *guess* that ID is declared as auto_increment primary key which means that jpgId should be declared as int.

    It's also possible that printed is not an int field, which it needs to be if you are going to compare it to the number 0.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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