473,387 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Avoiding the need to repeat clauses in MySQL

Thanks Rich - that's great. Before I found this group, I thought I had
a reasonable understanding of SQL (well, MySQL's implementation of it,
at least) but the truth is there are great chunks that I have no
knowledge of. I've read three or four books about MySQL and PHP all of
which gave (in retrospect) very basic examples of the use of SQL, and
focussed on features such as MySQL's date and time handling functions
that are well covered in PHP, so I came to the conclusion that
post-query processing with PHP was the way to go. I'm wishing now that
I had just sat and read the MySQL manual!

I'm constructing a query that searches through a list of people. Each
person has a firstname, lastname and businessname. Ordering is by
businessname, unless there is no record for businessname, in which case
the entry is ordered by lastname. Rich Ryan and Bill Karwin explain how
to do this in the following post

http://groups.google.co.uk/group/mai...dbea1fcccb53d0)

I'm also using the solution that Rich proposes in the previous post in
this thread to move 'The ' at the start of a business name to the end
of the name

and adding additional search clauses, for example, ordering by results
that start with a certain letter (the SQL below looks for results
starting with 't').

I initially thought that I would be able to use the first query below,
but MySQL responds that it does not recognise 'b' in the WHERE clause,
so I'm having to repeat the 'anti-The' CASE in every WHERE and ORDER BY
clause. Is there any way I can change this to avoid having to repeat
the CASE phrase?

If I use 'b' (as in the first SQL example below) in a simple ORDER BY
clause it works fine - the minute I use 'b' within a clause such as

COALESCE(
IF ( b = '', NULL , b ), CONCAT( ppl.ppl_lastname, ppl.ppl_firstname )
)

it no longer recognises 'b'.

Michael

Table: aa_people
Fields: ppl_id, ppl_salutation, ppl_firstname, ppl_lastname, ppl_dept,
ppl_email, ppl_businessname

----------------------------------------------------------------------------------------------------------

SQL query1:
SELECT ppl.ppl_id, ppl.ppl_salutation, ppl.ppl_firstname,
ppl.ppl_lastname, ppl.ppl_dept, ppl.ppl_email,

CASE WHEN SUBSTRING( ppl.ppl_businessname, 1, 4 ) = "The "
THEN CONCAT( SUBSTRING( ppl.ppl_businessname, 5, LENGTH(
ppl.ppl_businessname ) -4 ) , ", The" )
ELSE ppl.ppl_businessname
END

AS b

FROM aa_people ppl
WHERE 1

AND (
( b LIKE "t%" )
OR ( (b = "") && ( ppl.ppl_lastname LIKE "t%" ) )
)
ORDER BY
COALESCE(
IF ( b = '', NULL , b ), CONCAT( ppl.ppl_lastname, ppl.ppl_firstname )
)
----------------------------------------------------------------------------------------------------------
SQL query2:
SELECT ppl.ppl_id, ppl.ppl_salutation, ppl.ppl_firstname,
ppl.ppl_lastname, ppl.ppl_dept, ppl.ppl_email,

CASE WHEN SUBSTRING( ppl.ppl_businessname, 1, 4 ) = "The "
THEN CONCAT( SUBSTRING( ppl.ppl_businessname, 5, LENGTH(
ppl.ppl_businessname ) -4 ) , ", The" )
ELSE ppl.ppl_businessname
END

AS ppl_businessname

FROM aa_people ppl
WHERE 1
AND (
(

CASE WHEN SUBSTRING( ppl.ppl_businessname, 1, 4 ) = "The "
THEN CONCAT( SUBSTRING( ppl.ppl_businessname, 5, LENGTH(
ppl.ppl_businessname ) -4 ) , ", The" )
ELSE ppl.ppl_businessname
END

LIKE "t%"
)

OR
( (ppl_businessname = "") && ( ppl.ppl_lastname LIKE "t%" ) )

)

ORDER BY

COALESCE(

IF (
ppl_businessname = '', NULL ,

CASE WHEN SUBSTRING( ppl.ppl_businessname, 1, 4 ) = "The "
THEN CONCAT( SUBSTRING( ppl.ppl_businessname, 5, LENGTH(
ppl.ppl_businessname ) -4 ) , ", The" )
ELSE ppl.ppl_businessname
END

),

CONCAT( ppl.ppl_lastname, ppl.ppl_firstname )

)

Dec 24 '05 #1
2 1761
Sorry - I posted this as a new message, when actually it's a
continuation of the thread at

http://groups.google.co.uk/group/mai...94624b44297f05

Michael

Dec 24 '05 #2
<m.******@btinternet.com> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...
I initially thought that I would be able to use the first query below,
but MySQL responds that it does not recognise 'b' in the WHERE clause,


Correct. It is unfortunate -- but it is documented -- that one cannot use
column aliases in the WHERE clause. Apparently this is in accordance with
the SQL standard.

http://dev.mysql.com/doc/refman/5.0/en/select.html says:
A select_expr can be given an alias using AS alias_name. The alias is used
as the
expression's column name and can be used in GROUP BY, ORDER BY, or HAVING
clauses.
... It is not allowable to use a column alias in a WHERE clause, because
the column value
might not yet be determined when the WHERE clause is executed.
See Section A.5.4, "Problems with Column Aliases".

Also see http://dev.mysql.com/doc/refman/5.0/...ith-alias.html for
further explanation for why this restriction exists.

However, a user's comment on the latter page shows that you can use column
aliases in HAVING, GROUP BY, and ORDER BY clauses. The suggestion is that
in some cases, you can achieve the same end result by putting references to
your column alias in one or more of these clauses.

Regards,
Bill K.
Dec 24 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: | last post by:
As we're on this topic in another thread right now: Say I have a SELECT query from more than one table and with some = conditions, does it matter in what order I enter the tables in the FROM =...
1
by: Generale Cluster | last post by:
hello, I've made a template which has the layout I want, but there is an undesired white space between the left elements and the right column. How can I remove it? Thank you Bye!! Here's the...
4
by: Chuck Haeberle | last post by:
I have an interesting regular expression challenge for someone more experienced with them than I for a data layer class... I need an expression to search a SQL statement (any type, SELECT INSERT...
8
by: Mike Mascari | last post by:
Hello. I have a query like: SELECT big_table.* FROM little_table, big_table WHERE little_table.x = 10 AND little_table.y IN (big_table.y1, big_table.y2); I have indexes on both big_table.y1...
48
by: phillip.s.powell | last post by:
MySQL 3.23.58 - 4.0.17 (yep, several database server instances, don't ask) I have database Spring with table Students I have database Summer with table Students I am tasked to produce a...
27
by: galt_57 | last post by:
I need to do just a few multiplies of long integers and have a divide by ten. Can I avoid using floats? Can I use two longs as a 64 bit value somehow? Thanks.
0
by: Chuck36963 | last post by:
Hi all, I've been working on a listing problem and I can't figure out how to work it out. I have looked far and wide on the web to find answers, but I'd like other peoples input on my project in...
3
by: charchap | last post by:
hello. I'm experiencing problems with my css code (once again) working with Firefox and not working with IE 6 or 7 I have a problem with my head_logo_inner not too sure what's going on . Here is...
2
by: jdvictor | last post by:
Understandably I know that the real issue is in IE but at the moment everything looks good in IE and not Firefox. THE PROBLEM The problem that I am having is that my navigation menu is out of...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.