0

So here is a MySQL Query:

SELECT TestSite . * , LoggedCarts . * 
FROM TestSite, LoggedCarts
WHERE TestSite.email =  'LoggedCarts.Bill-Email'
LIMIT 0 , 30

It is returning an empty result set, when it should be returning four results based on the tables below.

First Table: LoggedCarts - Column: Bill-Email

[email protected]
[email protected]

Second Table: TestSite - Column: email

[email protected]
[email protected]
[email protected]
[email protected]
[email protected]

The goal is to get a MySQL statement that returns the rows in Table: TestSite that don't match the rows in Table: LoggedCarts.

Note: I understand that the use of a hyphen in a column name requires special care when constructing a query, involving backticks to tell MySQL there are special characters. I would change the column names to match up, however the Table: LoggedCarts has data fed via post from a Yahoo Shopping Cart and without heavy preparation before insertion setting the name to anything but the key sent in the post data is daunting.

However, if it turns out rebuilding the data prior to insertion is easier than using a JOIN statement or for some reason using two columns with different names as the comparison columns just doesn't work, I will go through and rebuild the database and PHP code.

5
  • 1
    is "LoggedCarts.Bill-Email" supposed to be a string? Commented Dec 11, 2013 at 20:06
  • 1
    You've potentially answered your own question ...special care when constructing a query, involving backticks to tell MySQL there are special characters. Commented Dec 11, 2013 at 20:08
  • Stop tagging MySQL questions with SQL Server please. Commented Dec 11, 2013 at 20:13
  • Removed the extra category tags. Backticks around the column identifier resulted in an unknown column error, whereas apostrophes resulted in the join occuring but all of the LoggedCart table values were NULL. Commented Dec 11, 2013 at 20:22
  • FROM TestSite, LoggedCarts is not a "full join" (which is the same as "full outer join" - something MySQL can not do) - it's a cross join. Commented Dec 11, 2013 at 21:00

2 Answers 2

3

Single quotes indicate a string literal. You need to use backticks for identifiers. Also, each component of an identifier must be quoted individually.

SELECT TestSite . * , LoggedCarts . * 
FROM TestSite, LoggedCarts
WHERE TestSite.email =  LoggedCarts.`Bill-Email`
LIMIT 0 , 30

From the manual:

If any components of a multiple-part name require quoting, quote them individually rather than quoting the name as a whole. For example, write `my-table`.`my-column`, not `my-table.my-column`.

Sign up to request clarification or add additional context in comments.

2 Comments

The backticks resulted in an Unknown column error. Apostrophes allow the join to happen, but the values from LoggedCarts are all NULL.
That did it, now I just have to reverse it with a != or <> operator. Thank you!
0

With a bit of research inspired by somne of the hints given, I found the solution I was looking for here: SELECT * WHERE NOT EXISTS

Does exactly what I need it to do, and as a bonus, I like the shorthand syntax that is used that allows you to put in an alias for the table name and use the alias throughout the statement.

SELECT *
FROM TestSite e
WHERE NOT EXISTS
    (
    SELECT  null 
    FROM    LoggedCarts d
    WHERE   d.`Bill-Email` = e.email
    )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.