Breaking Down Queries To Be More Readable
Sometimes queries start getting crazy complicated and complex looking. You'll start looking at a massive query and think, "WTF is going on here?"
My strategy for making a query look more simple and laid out better is like the following:
select
n.lastName,
a.zip
from
names n
left join
addresses a
on
n.id=a.nameId
where
zip in ("90000", "90001");
I don't know if this will work for you, but it really helps me when I start getting dizzy from queries, and let me tell you, there are some INSANE queries out there.
Some examples of queries are you can join, more than just two tables together. You can join as many as you want, using whatever join type you need to use. You can also do queries within queries. Sometimes it's ridiculous the lengths some people will go to get the data they need from the database.
After a while, you'll be a pro at this stuff, and it won't be a big deal. I rarely ever have to do my query break out method to see the queries anymore. I usually just write one long line of text and my brain can extrapolate from there. But, you might not have that super power yet, and it's okay to break queries out like that. In fact, for a long time, i exclusively wrote my queries in PHP over multiple lines like I did above.
$query = "
select
n.lastName,
a.zip
from
names n
left join
addresses a
on
n.id=a.nameId
where
zip in ("90000", "90001");
";
Is perfectly valid PHP code. MySQL will expect that as a perfectly valid query. In fact, if you were to copy and paste the entire query into your MySQL CLI, it will run perfectly. So, don't hesitate to experiment.
Of course, the above $query is AWFUL for security in PHP. So, don't use that as is. We'll be looking at that more later.
Here's another one that I recently did:
select c.id, unix_timestamp(c.created) as created, pc.company_name, pc.code as company_code, ca.name as account, cc.name as client, c.monthly, c.daily, c.cost_per_item, c.active, c.submit_mode,
v.name as vertical, (select count(*) from product_http_send chs where c.id=phs.product_id) as committed from
product as c left join post_company pc on c.post_company_id=pc.id left join product_account ca on c.account_id=ca.id left join
product_client cc on c.client_id=cc.id left join vertical v on c.vertical_id=v.id where c.monthly='August' order by pc.company_name limit 0, 20
Can be broken down like:
select
c.id,
unix_timestamp(c.created) as created,
pc.company_name,
pc.code as company_code,
ca.name as account,
cc.name as client,
c.monthly,
c.daily,
c.cost_per_item,
c.active,
c.submit_mode,
v.name as vertical,
(
select count(*)
from product_http_send phs
where c.id=phs.product_id
) as committed
from
product as c left join post_company pc on c.post_company_id=pc.id
left join product_account ca on c.account_id=ca.id
left join product_client cc on c.client_id=cc.id
left join vertical v on c.vertical_id=v.id
where
c.monthly='August'
order by
pc.company_name
limit
0, 20
Don't worry about trying to understand the above query. It's a complicated query. This is more designed to show how to break down a complicated query into something that might feel more readable to you.