Vincent Vincent - 2 months ago 17
SQL Question

How to make long sql sentence look beautiful in PHP(or other language)?

This is an example; For looking comfortable I fill some tab and line feed in it; I'm looking for some better ways except using '.' to link them.

$str_sql = "SELECT
sc.`id` AS 'payment_id',
sc.`order_id` AS 'order_id',
od.`partner_id` AS 'partner_id',
pt.`partner_name` AS 'partner_name',
od.`term` AS 'order_terms',
od.`has_pay` AS 'paid_terms',
sc.`sort_order` AS 'current_term',
FROM_UNIXTIME(sc.`repayment_time`) AS 'payment_time',
FROM_UNIXTIME(sc.`receive_time`) AS 'pay_time',
sc.`order_price` AS 'term_price',
od.`add_time` AS 'order_time',
sc.`status` AS 'status',
od.`order_price` AS 'order_price',
usr.`user_name` AS 'user_name',
usr.`id` AS 'user_id',
usr.`user_cell` AS 'user_cellphone',
IF(sc.`status` IN (0,3), 1 , 0 ) AS 'overdue',

mayidev.`lzh_order_payment_schedule` sc
JOIN lzh_order od
ON sc.`order_id` = od.`id`
JOIN lzh_partners pt
ON od.`partner_id` = pt.`id`
JOIN lzh_order_user usr
ON = usr.order_id
JOIN lzh_partner_overdue_interest po
ON po.`partner_id` =
WHERE sc.`repayment_time` < 1477441800
AND sc.`receive_time` = 0
AND sc.`status` != 1
AND od.`status` = 4 ;";


The best way to make long SQL sentence look beautiful in PHP is to separate SQL code from PHP code. And the best way to do that is create views and stored procedures in SQL to get the data. You don not really need all this JOINs logic in PHP. It is best kept on the SQL server to keep the code maintainable.

And on PHP side you can just SELECT * FROM view or CALL procedure().