TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 02-20-2008, 09:42 AM   #1 (permalink)
The Frequenter
Prolific Welcomer Upcoming Programmer 
 
Join Date: Sep 2007
Posts: 360
Thanks: 24
Haris is on a distinguished road
Default Indenting SQL

How do you indent your SQL?

I think there might be a proper standard since Wildhoney separates every clause on different line.
__________________
Necessity is the mother of invention.

My blog
Haris is offline  
Reply With Quote
Old 02-20-2008, 02:01 PM   #2 (permalink)
The Acquainted
 
Gareth's Avatar
 
Join Date: Jan 2008
Posts: 136
Thanks: 4
Gareth is on a distinguished road
Default

sql Code:
SELECT * FROM
    sui_users
WHERE
    username = '$username'
AND
    pass = '$md5password'
Gareth is offline  
Reply With Quote
Old 02-20-2008, 02:22 PM   #3 (permalink)
The Acquainted
 
Join Date: Nov 2007
Posts: 154
Thanks: 31
SOCK is on a distinguished road
Default

Just hit your <enter> key and use the spacebar to indent. I've not seen a standard per se as to how to properly format SQL statements, but it makes it much more clear (and easy to edit) if it's not all bunched together.
__________________
I reject your reality, and substitute my own.
SOCK is offline  
Reply With Quote
Old 02-20-2008, 03:58 PM   #4 (permalink)
The Wanderer
 
Join Date: Feb 2008
Posts: 10
Thanks: 0
Pete is on a distinguished road
Default

I tend to put a tab line in and start each part of the statment on the tab line so if ofen looks like:
Code:
   $myquery=    "SELECT * FROM table
                WHERE something ='something'"
so like SOCK says but use the tab button, I like to have it in a line so its line1: selecting what, line2: criteria or results, and line3+: my orderby/sortby.

obviously if its a more complex query it changes with joins and what not but thats my basic format for queries.
Pete is offline  
Reply With Quote
Old 02-20-2008, 07:38 PM   #5 (permalink)
The Acquainted
 
Join Date: Nov 2007
Posts: 154
Thanks: 31
SOCK is on a distinguished road
Default

Now, if you're talking about SQL within a PHP script, I find the overall best solution (not in every case, mind you) is to use HEREDOC syntax, e.g.
PHP Code:

$sql 
= <<< END_SQL
SELECT
 column1
, column2
, date_column
FROM sometable
WHERE pkID = 1024
END_SQL; 
The limitation is that the end statement (in this case END_SQL, can be anything you want) has to be on a non-indented line. So it's great for a script that doesn't have a bunch of conditional blocks that have to be intented. Unfortunately the syntax highlighter has a problem so it doesn't 'pop' as it should, but it works great. Easy to edit, not to mention allowing you the ability to have a HUGE multi-table JOIN that you can actually decipher.

Second best method, IMHO is to simply use PHP's ability to ignore whitespace as Pete pointed out.

PHP Manual Language Reference | Types : Strings | HEREDOC syntax
__________________
I reject your reality, and substitute my own.
SOCK is offline  
Reply With Quote
Old 02-29-2008, 05:19 PM   #6 (permalink)
Nor
The Addict
 
Join Date: Nov 2007
Posts: 282
Thanks: 61
Nor is on a distinguished road
Default

Heredocs aren't great because when you close a heredoc string you must not have any white space in front of it which is a problem is most cases when trying to make your code look clean.
__________________
PHP/XHTML Freelancer:
Cleanscript.com v3 - Programming starting at just $5 act now!
Nor is offline  
Reply With Quote
Old 02-29-2008, 06:07 PM   #7 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
Alan @ CIT is on a distinguished road
Default

I just tend to split mine over multiple lines if it's a long query to make it more readable than 1 long line.

For example:

Code:
SELECT `a`.`articleid`, `a`.`title`, `a`.`content`, `m`.* 
FROM `articles` AS `a` 
INNER JOIN `authors` AS `m` 
ON a.authorid = m.authorid 
WHERE (m.name = 'Alan') AND (a.created > 1204205135) 
ORDER BY `title` ASC
As long as it's readable it doesn't really matter how you format it

Alan
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
Old 02-29-2008, 11:22 PM   #8 (permalink)
The Acquainted
 
wGEric's Avatar
 
Join Date: Nov 2007
Posts: 166
Thanks: 0
wGEric is on a distinguished road
Default

Here's how I do it.

sql Code:
SELECT col1, col2
    FROM TABLE
    WHERE col = 1
        AND col2 = 'foo'
    ORDER BY col3 ASC
    LIMIT 20
__________________
Eric
wGEric is offline  
Reply With Quote
Old 12-16-2009, 10:55 AM   #9 (permalink)
The Visitor
 
Join Date: Dec 2009
Posts: 1
Thanks: 0
Francisco is on a distinguished road
Default

poor example, but here is how i do it.

Code:
SELECT t1.col1, t1.col2, t3.col3
  FROM tab1 as t1, tab2 as t2
       LEFT JOIN tab3 as t3 on t3.colx = t1.col1
 WHERE t1.col1 = cond1
   AND t1.col2 = cond2
 GROUP BY t1.col1, t1.col2
 ORDER BY t1.col1, t1.col2 ASC
HAVING func(t1.col1)
Francisco is offline  
Reply With Quote
Old 09-14-2010, 09:19 AM   #10 (permalink)
The Visitor
 
Join Date: Sep 2010
Posts: 1
Thanks: 0
chrisadam is on a distinguished road
Default Indenting SQL

SQL standard

In general the SQL92 standard can be followed. Support for this in various databases are in most cases complete. This document will explain in which cases the standard cannot be followed.


__________________________________________________ _____________

Want to get-on Google's first page and loads of traffic to your website? Hire a SEO specialist from Ocean Groups seo specialist
chrisadam is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 01:29 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design