Why does this left outer join query refuse to complete in phpMyAdmin?
EDITED: so the query does work, but on my localhost machine it took over a minute to run. Admittedly the tables are considerably larger on my localhost (about 8000 rows each), but in production the tables could have upwards of 25,000 rows each. Is there any way to optimize this so it doesn't take as long? As indicated in one of the comments, bot开发者_JAVA技巧h tables are indexed.
I have two tables, jos_eimcart_customers_addresses and jos_eimcart_customers. I want to pull all records from the customers table, and include address information where available from the addresses table. I have what I thought was a fairly ordinary left outer join query, but it keeps timing out in phpMyAdmin, even though there aren't that many results it should be finding. Can anyone point out if I'm doing something wrong? I'm not getting a mySQL error.
select
c.firstname,
c.lastname,
c.email as customer_email,
a.email as address_email,
c.phone as customer_phone,
a.phone as address_phone,
a.company,
a.address1,
a.address2,
a.city,
a.state,a.zip,
c.last_signin
from jos_eimcart_customers c
left outer join jos_eimcart_customers_addresses a
on c.id = a.customer_id
order by c.last_signin desc
You need a group by clause:
SELECT
c.firstname,
c.lastname,
c.email AS customer_email,
a.email AS address_email,
c.phone AS customer_phone,
a.phone AS address_phone,
a.company,
a.address1,
a.address2,
a.city,
a.state,
a.zip,
c.last_signin
FROM jos_eimcart_customers c
LEFT OUTER JOIN jos_eimcart_customers_addresses a ON c.id = a.customer_id
GROUP BY
c.firstname,
c.lastname,
c.email AS customer_email,
a.email AS address_email,
c.phone AS customer_phone,
a.phone AS address_phone,
a.company,
a.address1,
a.address2,
a.city,
a.state,
a.zip,
c.last_signin
ORDER BY c.last_signin DESC
There is nothing wrong with your query (assuming the counts are as you say they are). Something else is going on in your server.
Any chance it is waiting on a lock?
精彩评论