Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
sql help; select 1st letter of last name
Old 05-14-2005, 01:03 AM sql help; select 1st letter of last name
Skilled Talker

Posts: 69
Trades: 0
Hello,

I am trying to write a sql statement that will select the first letter of the last name. The name, unfortunately is all run together with no rhyme or reason....ie) john doe or john h doe or john henry doe.

$sql="SELECT * from webpagetable WHERE (first letter of last name) = m OR M";

Can anyone offer any guidance or place on net for code?

Regards,
Matt
weddingm is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 05-14-2005, 05:48 AM
OmuCuSucu's Avatar
Vi Veri Veniversum Vivus

Posts: 1,168
Name: Dragos-Valentin
Location: Cluj-Napoca, RO
Trades: 0
i'd say the best approach would be having a separate field for last mane and checking with m% or M% ... i can't think of any statement for that right now.

you could use php to find the last space in the name and return the next character (firt letter of last name) but i think you would be better off redesigning the db structure.

just an opinion.
__________________
.
» Please remember to add to my Talkupation if you enjoyed my post. Thank you :)
.
OmuCuSucu is offline
Reply With Quote
View Public Profile
 
Old 05-14-2005, 12:32 PM
leavethisplace's Avatar
Ultra Talker

Posts: 297
Trades: 0
Is the second name stored in a seperate cell to the first name, or is the whole name in one cell?
__________________
A lie gets halfway around the world before the truth has a chance to get its pants on. - Sir Winston Churchill

Please visit my sites:
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
leavethisplace is offline
Reply With Quote
View Public Profile
 
Old 05-14-2005, 06:29 PM
Logical Program's Avatar
Super Talker

Posts: 130
Location: Atlanta, Georgia
Trades: 0
Simple solution...

PHP Code:
// You can add a WHERE clause, or change the selection depending on what you need.
    
$sql "SELECT last_name from webpagetable";
$query mysql_query($sql);
$names mysql_fetch_array($queryMYSQL_NUM)

foreach (
$names as $key => $val){
    
$segments explode(' '$val);
    
$values count($segments)-1;
    
$last_initial ucwords(substr($segments[$values], 0,1));

__________________

Please login or register to view this content. Registration is FREE
- For all of your website programming and design needs, make the logical choice. Logical Assistance For Real-World Clients.
Logical Program is offline
Reply With Quote
View Public Profile Visit Logical Program's homepage!
 
Old 05-15-2005, 10:07 PM
Skilled Talker

Posts: 69
Trades: 0
the name is all in one cell
weddingm is offline
Reply With Quote
View Public Profile
 
Old 05-15-2005, 10:55 PM
Christopher's Avatar
Iced Cap

Latest Blog Post:
Cross-domain AJAX with JSONP
Posts: 3,110
Location: Toronto, Ontario
Trades: 0
Use SUBSTRING() to get the first letter, I think.

Code:
SELECT * FROM webpagetable WHERE SUBSTRING(lastname, 0, 1) = 'm' OR SUBSTRING(lastname, 0, 1) = 'M'
Logical Program, that's a solution I wouldn't want to use. You would be reading all rows into memory, then looping through all of them. If you had lots of rows, it would take an enormous amount of resources and take a lot of time to complete.

// Edit
Oh, the name is all in one field. Then I think you might be able to use SUBSTRING_INDEX combined with SUBSTRING. You can try it

I think using regex would be good in this situation. I don't have time to test out an expression right now, though. Hopefully that'll help at least a little.
__________________

Please login or register to view this content. Registration is FREE
- Latest Articles:
Please login or register to view this content. Registration is FREE
,
Please login or register to view this content. Registration is FREE

--
Please login or register to view this content. Registration is FREE

Christopher is offline
Reply With Quote
View Public Profile
 
Old 05-16-2005, 08:08 AM
ibbo's Avatar
Super Spam Talker

Posts: 880
Location: Leeds UK
Trades: 0
you could also use a simple match like :

select * from TABLE where last_name like 'A%'

which would also return all entries where last name starts with a A.

ibbo
__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

Linux user #349545 :
(GNU/Linux)iD8DBQBAzWjX+MZAIjBWXGURAmflAKCntuBbuKCWenpm XoA7LNydllVQOwCf
ibbo is offline
Reply With Quote
View Public Profile Visit ibbo's homepage!
 
Reply     « Reply to sql help; select 1st letter of last name
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.50715 seconds with 12 queries