Getting a reversed result

I am running a MySql query but I am getting too many results (600 rows on a 150 row table)

Table A = id, pid, image
Table B = pid, imageA, imageB

$images = “SELECT a.* FROM tableA a, tableB b WHERE a.pid = ‘7’ AND a.image <> b.imageA OR a.image <> b.imageB;”

I am trying to get an image name from tableA if it does not exist in tableB

When I run this I seem to get more rows from table A when it should in theory be less rows depending on the AND references.

the error is caused by mixing ANDs and ORs in ways you weren’t anticipating

but best way to do what you want is with LEFT OUTER JOIN and COALESCE

SELECT TableA.pid , COALESCE(TableB.imageA,TableB.imageB,TableA.image) AS image FROM TableA LEFT OUTER JOIN TableB ON TableB.pid = TableA.pid AND TableA.image IN (TableB.imageA,TableB.imageB)

Many thanks

Still showing the id though where image is equal to imageA or imageB

you said ~if~ it does not exist in tableB

i think now you meant to say ~only if~

please rephrase what you want

Yeah your code works using not in rather than in, I got it the wrong way round as I didn’t explain properly.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.