I have a big query with a lot of inner join inside in a page on my website.
I want to apply for some left outer join limit 1 but if I put it into the sql give me errors.
This is a small part of my query isn’t total, this query is only a part and ISN’T FINISH:
SELECT * FROM products
LEFT OUTER JOIN (origins
LEFT OUTER JOIN origin_aliases ON origin_aliases.origin_id = origins.id
AND origin_aliases.language = '$lang'
LEFT OUTER JOIN (diets LEFT OUTER JOIN diet_aliases ON diet_aliases.diet_id = diets.id AND diet_aliases.language = '$lang')
ON origins.diet_id = diets.id)
ON products.origin_id = origins.id
LEFT OUTER JOIN (product_aliases
LEFT OUTER JOIN (users as users1
LEFT OUTER JOIN profiles as profiles1 ON profiles1.user_id = users1.id
LEFT OUTER JOIN user_options as user_options1 ON user_options1.user_id = users1.id
LEFT OUTER JOIN avatars as avatars1 ON avatars1.user_id = users1.id
)
ON product_aliases.user_id =users1.id
LEFT OUTER JOIN (users as users2
LEFT OUTER JOIN profiles as profiles2 ON profiles2.user_id = users2.id
LEFT OUTER JOIN user_options as user_options2 ON user_options2.user_id = users2.id
LEFT OUTER JOIN avatars as avatars2 ON avatars2.user_id = users2.id
)
ON product_aliases.edit_user_id = users2.id)
ON product_aliases.product_id = products.id
…..
For example I want to apply limit 1 in the left outer join in origins after ON 'products.origin_id'='origins.id' is possible? How can I do this?
I have tried to put after the on condition limit 1 but give me an error
Syntax error or access violation: 1064 You have an error in your SQL
syntax; check the manual that corresponds to your MySQL server version
for the right syntax
As written, your example is full or errors. Your derived tables do not have
SELECTclauses, you have quoted the table and column references, and it’s not clear if names like origins and origin_aliases are tables or aliases. Those are your syntax errors.Once you correct those, you can add the
LIMITexpression to the entire query or to any of the derived tables. Here is my best guess:I choose to use two table aliases (a and b). And I’m assuming that origins, origin_aliases, and diets are tables. Note you should not use
SELECT *; provide the column names you want from each derived table as well as in your mainSELECTclause.