Unix Technical Forum

Re: Query Help

This is a discussion on Re: Query Help within the MySQL forums, part of the Database Server Software category; --> On 8 Nov, 14:13, jc...@lycos.com wrote: > $query = "SELECT DISTINCT names > FROM $table > Order by names"; ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #11 (permalink)  
Old 02-28-2008, 11:29 AM
Captain Paralytic
 
Posts: n/a
Default Re: Query Help

On 8 Nov, 14:13, jc...@lycos.com wrote:
> $query = "SELECT DISTINCT names
> FROM $table
> Order by names";
> $result = mysql_query($query);
>
> In the code above, 'Order by' works very well when not used in
> conjunction with other query-able items but how about when used in a
> query where other possible choices are included? How do I use 'Order
> by' in the query below where it will only act on 'names' when "All"
> is
> selected and otherwise, not really be active during the query as in
> the code below? I'm using PHP to access MySQL. Thanks
>
> $query = "SELECT *
> FROM $table
> WHERE 1 = 1 ";
> if($year != "All") $query .= "and year = '".$year."'";
> if($status != "All") $query .= "and status = '".
> $status."'";
> if($names != "All") $query .= "and names = '".$names."'";
> $result = mysql_query($query);


ORDER BY names

the whole WHERE clause is superfluous in this query.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #12 (permalink)  
Old 02-28-2008, 11:29 AM
Steve
 
Posts: n/a
Default Re: Query Help


"Captain Paralytic" <paul_lautman@yahoo.com> wrote in message
news:1194534736.193031.304830@q5g2000prf.googlegro ups.com...
> On 8 Nov, 14:13, jc...@lycos.com wrote:
>> $query = "SELECT DISTINCT names
>> FROM $table
>> Order by names";
>> $result = mysql_query($query);
>>
>> In the code above, 'Order by' works very well when not used in
>> conjunction with other query-able items but how about when used in a
>> query where other possible choices are included? How do I use 'Order
>> by' in the query below where it will only act on 'names' when "All"
>> is
>> selected and otherwise, not really be active during the query as in
>> the code below? I'm using PHP to access MySQL. Thanks
>>
>> $query = "SELECT *
>> FROM $table
>> WHERE 1 = 1 ";
>> if($year != "All") $query .= "and year = '".$year."'";
>> if($status != "All") $query .= "and status = '".
>> $status."'";
>> if($names != "All") $query .= "and names = '".$names."'";
>> $result = mysql_query($query);

>
> ORDER BY names


just a note about legibility here...

$sql[] = "
SELECT *
FROM " . $table . "
WHERE 1 = 1
";
if ($year != 'ALL')
{
$sql[] = " AND year = '" . $year . "'";
}
if ($status != 'ALL')
{
$sql[] = " AND status = '" . $status. "'";
}
if ($names != 'ALL')
{
$sql[] = " AND names = '" . $names. "'";
}
$sql = implode("\r\n", $sql);

that avoide the problem of forgetting to put a space between the if'd
conditions/criterion. note, i put the spaces before 'and' simply so that
when i go to debug the current sql being run, it echos nice and formatted in
the browser.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #13 (permalink)  
Old 02-28-2008, 11:29 AM
Darko
 
Posts: n/a
Default Re: Query Help

On Nov 8, 4:34 pm, "Steve" <no....@example.com> wrote:
> "Captain Paralytic" <paul_laut...@yahoo.com> wrote in message
>
> news:1194534736.193031.304830@q5g2000prf.googlegro ups.com...
>
>
>
> > On 8 Nov, 14:13, jc...@lycos.com wrote:
> >> $query = "SELECT DISTINCT names
> >> FROM $table
> >> Order by names";
> >> $result = mysql_query($query);

>
> >> In the code above, 'Order by' works very well when not used in
> >> conjunction with other query-able items but how about when used in a
> >> query where other possible choices are included? How do I use 'Order
> >> by' in the query below where it will only act on 'names' when "All"
> >> is
> >> selected and otherwise, not really be active during the query as in
> >> the code below? I'm using PHP to access MySQL. Thanks

>
> >> $query = "SELECT *
> >> FROM $table
> >> WHERE 1 = 1 ";
> >> if($year != "All") $query .= "and year = '".$year."'";
> >> if($status != "All") $query .= "and status = '".
> >> $status."'";
> >> if($names != "All") $query .= "and names = '".$names."'";
> >> $result = mysql_query($query);

>
> > ORDER BY names

>
> just a note about legibility here...
>
> $sql[] = "
> SELECT *
> FROM " . $table . "
> WHERE 1 = 1
> ";
> if ($year != 'ALL')
> {
> $sql[] = " AND year = '" . $year . "'";}
>
> if ($status != 'ALL')
> {
> $sql[] = " AND status = '" . $status. "'";}
>
> if ($names != 'ALL')
> {
> $sql[] = " AND names = '" . $names. "'";}
>
> $sql = implode("\r\n", $sql);
>
> that avoide the problem of forgetting to put a space between the if'd
> conditions/criterion. note, i put the spaces before 'and' simply so that
> when i go to debug the current sql being run, it echos nice and formatted in
> the browser.


You and your formatting again You won't give up until you educate
all newbies to
proper formatting, eh?

Cheers

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #14 (permalink)  
Old 02-28-2008, 11:29 AM
Steve
 
Posts: n/a
Default Re: Query Help


"Captain Paralytic" <paul_lautman@yahoo.com> wrote in message
news:1194624587.319538.252680@v2g2000hsf.googlegro ups.com...
> On 9 Nov, 14:08, "Steve" <no....@example.com> wrote:
>> hmmm. so i was in a thread dealing with a particular subject and posted a
>> reply to a previous post within the thread, dude. did you want to make a
>> dashing, stand-out point here.

>
> And you posted the reply attached to the wrong previous post


'wrong'? no, i responded to the one i intended to respond to.

>> people do what they like

> People like you obviously do. Others have more sense.


'people like me'? you mean those who aren't so bent up about fictious or
preceived rules? and the ones with more sense, those who enjoy the binding
that holds to tightly bundled sticks together. (based on your iq, i doubt
you even get to what that refers.)

>> the irony meter just went of the
>> scale as soon as you tried to talk about things you obviously have *no*
>> idea
>> work.

> Now that's ironic coming from you!


really? here's a clue. when hurling insults that actually hit a target, it
is best to provide examples...like when you say things like usenet does this
or that when it actually doesn't. see, that's so well delivered that the
hilarity is just a side-line bene. the meat of the comment was to show you
have no idea about what you're trying to discuss.

on that note, please inform us all of what irony i specifically provide. if
you don't, we call that a red-herring coupled with ad-homonym - a diversion
from your previous embarrassment.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
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
Forum Jump


All times are GMT. The time now is 06:18 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com