Here I have two tables in MySQL 5.6...
CREATE TABLE Zones (
ZoneID int,
ZoneName varchar(10),
AccountID int,
ProxyType int
);
CREATE TABLE ProxyServers (
ProxyID int,
ProxyType int,
ProxyIP varchar(10),
ProxyPort int,
Enabled tinyint
);
My structure is a lot more complex than the one outlined about but I am trying to keep this as simple as possible to explain.
Basically I want to return 1 row of data consisting of Zones.ID, Zones.ZoneName, and a ProxyID selected at random joined by ProxyType
SELECT * FROM Zones z
LEFT JOIN ProxyServers ps ON ps.ProxyID = (
SELECT ProxyID FROM ProxyServers WHERE ProxyType = z.ProxyType ORDER BY RAND() LIMIT 1
)
ORDER BY ZoneID DESC
LIMIT 1
This is simple enough to do in T-SQL, but can't figure it out in MySQL. Plus what is rather strange, even thought the data set does not change, the results are different ever time the query is run. Sometimes no rows, sometimes 4, sometimes even duplicate rows. I don't get it :/ I think it's the ORDER BY RAND() causing this because when I remove that it's fine.