Skip to content
Advertisement

Return only one row in subquery

I have the following query that I’m executing to show these records on a Laravel view.

I’m having issues with this line

Because there are a lot of cities with the same name in my database, so it’s throwing an error, (probably the same with states but the error is being thrown at cities in this case, I tried to add LIMIT 1 at the end of the subquery but it’s giving me an error, so I guess I can’t use LIMIT in a subquery?

This is what i tried to change

This is how the tables look like

companies table_companies

addresses

adddress_table

cities

cities

states

states

countries

enter image description here

How could I fix this?

Advertisement

Answer

You should really write your entire query as a JOIN. If it returns multiple rows it will be fairly easy to see where your duplication issue is. Note that based on what you have written, it is not due to cities having the same name, as you never select based on names. Without seeing data structures it’s hard to be certain but this should be close:

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement