I’m breaking my head on this simple peace of code, that doesn’t want to work, and I can’t think of any other solution…
Could you please help to make it work?
SELECT chart FROM chart WHERE (select count(user_id) FROM users join charts ON
user_id=charts.UID and charts.chart=chart WHERE INET_NTOA(user_ip)='127.0.0.1')=0;
There are 3 tables involved with the following columns:
chart(
chart int(5)
);
charts(
UID int(11),
chart int(5)
);
`users` (
`user_id` int(11),
`user_ip` int(10)
);
The point is that the ‘chart’ column from first select should be passed to subquery to charts.chart= chart, but instead the subquery ends up comparing with itself i.e. chart=chart – always true.
I know it wont work like that… but I can’t think of any other way really. Is there any way at all to the thing that I’m trying to do?
EDIT 1:
Basically I need a reverse selection:
select chart.chart from chart join charts on chart.chart=charts.chart join users
on user_id=charts.UID and INET_NTOA(user_ip)='127.0.0.1';
This query returns charts that are tagged by user with given ip, eg: 1, 4,5, 9.
But I need to select the ones that are not tagged instead, that is: 2,3,6,7,8,10 and so on…
EDIT 2:
I’m looking for some sort of negative join now. I think this would do, but I’m clueless how to use it. Meanwhile making some research…
Try:
Note that instead of
INET_NTOA(user_ip)='127.0.0.1'it’s much better to useuser_ip = 2130706433which is equivalent but also index friendly.