473,769 Members | 3,352 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_lastnam e, ppl.ppl_firstna me )
)

it no longer recognises 'b'.

Michael

Table: aa_people
Fields: ppl_id, ppl_salutation, ppl_firstname, ppl_lastname, ppl_dept,
ppl_email, ppl_businessnam e

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

SQL query1:
SELECT ppl.ppl_id, ppl.ppl_salutat ion, ppl.ppl_firstna me,
ppl.ppl_lastnam e, ppl.ppl_dept, ppl.ppl_email,

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

AS b

FROM aa_people ppl
WHERE 1

AND (
( b LIKE "t%" )
OR ( (b = "") && ( ppl.ppl_lastnam e LIKE "t%" ) )
)
ORDER BY
COALESCE(
IF ( b = '', NULL , b ), CONCAT( ppl.ppl_lastnam e, ppl.ppl_firstna me )
)
----------------------------------------------------------------------------------------------------------
SQL query2:
SELECT ppl.ppl_id, ppl.ppl_salutat ion, ppl.ppl_firstna me,
ppl.ppl_lastnam e, ppl.ppl_dept, ppl.ppl_email,

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

AS ppl_businessnam e

FROM aa_people ppl
WHERE 1
AND (
(

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

LIKE "t%"
)

OR
( (ppl_businessna me = "") && ( ppl.ppl_lastnam e LIKE "t%" ) )

)

ORDER BY

COALESCE(

IF (
ppl_businessnam e = '', NULL ,

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

),

CONCAT( ppl.ppl_lastnam e, ppl.ppl_firstna me )

)

Dec 24 '05 #1
2 1790
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.******@btint ernet.com> wrote in message
news:11******** *************@g 49g2000cwa.goog legroups.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
1710
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 = clause and in what order the WHERE conditions appear in my query? Or = does it make any difference if I use WHERE or HAVING? (I see that MS = Access likes those HAVINGs...) Of course my tables contain (maybe very much) more than some 100 records...
1
4330
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 code: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
4
1743
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 UPDATE OR DELETE) and find all single apostrophes which should be replaced with a double apostrophe without affecting the apostrophes used to delimit the string values: Example: SELECT field FROM table WHERE stringfield = 'This is one of...
8
3600
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 and big_table.y2 and on little_table.x and little_table.y. The result is a sequential scan of big_table. In order to prevent this, I've rewritten the query as:
48
3876
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 query of all students in both tables with no duplicates. No clue whatsoever.
27
2875
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
2451
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 the whole. I really need MySQL wizz to give me a hand (and maybe refer me to books to get me to the wizz level myself). First off, english is a second language to me and sometimes my sentences might be a little awkward. Please forgive me. Mon...
3
4220
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 my css for this particular template. body{ background-color: #333333; color: #333333; margin: 0px; padding: 0px; font-size: 0.8em;
2
1618
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 wack when viewing it in Firefox the browser seems to be pushing everything on the right to the left and I can't for the life of me solve the problem. Basically the menu is suppose to be sitting on the right side of the website but when viewing the...
0
9416
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10032
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9849
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8861
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7393
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6661
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5293
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5433
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2810
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.