Exphor
March 5, 2018, 10:04pm
1
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.
r937
March 5, 2018, 11:06pm
2
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)
Exphor
March 6, 2018, 10:16am
3
Many thanks
Still showing the id though where image is equal to imageA or imageB
r937
March 6, 2018, 10:38am
4
you said ~if~ it does not exist in tableB
i think now you meant to say ~only if~
please rephrase what you want
Exphor
March 6, 2018, 10:15pm
5
Yeah your code works using not in rather than in, I got it the wrong way round as I didn’t explain properly.
system
Closed
June 6, 2018, 5:16am
6
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.