Maybe if you were not so eager to shorten the code and forgot you can "rename" a table for the query, and if you used JOIN:
SELECT words.id, words.definition
FROM words
JOIN types ON words.typeID = types.id
JOIN stockmatch ON words.id = stockmatch.wordID
JOIN stock ON stockmatch.stockID = stock.id
JOIN languages ON stock.languageID = languages.id
WHERE stock.word = ?
AND languages.name = ?
AND types.name = ?
or
SELECT words.id, words.definition
FROM words
JOIN types ON words.typeID = types.id AND types.name = ?
JOIN stockmatch ON words.id = stockmatch.wordID
JOIN stock ON stockmatch.stockID = stock.id AND stock.word = ?
JOIN languages ON stock.languageID = languages.id AND languages.name
+ = ?
(Now, maybe I'm making a fool of myself. I've worked with Oracle and MS SQL, never with mysql, so I don't know it it allows this syntax.)
On the other hand ... once one would get used to your "language" he might be able to put the query together quicker than in SQL. (Even if only thanks to the fact it's fewer characters). But what if I wanted to use some "advanced" feature of the SQL server? Any Column in (SELECT Column FROM Table WHERE ...) or WHERE exists (SELECT * FROM Table WHERE ...) or ...
If I stay with SQL then it's much easier to start using those.
Also (again possibly nonsense, does mysql allow stored procedures?) it's generaly considered better to put especialy the complex queries into stored procedures. That way you do not force the server to recompile the query and regenerate the execution plan every time. Yes $dbh->prepare() helps, but still isn't perfect. Each process has to submit the query for compilation at least once.
Jenda |