Reply
MYSQL Select query from a comma delimited field?
Old 09-21-2009, 03:39 AM MYSQL Select query from a comma delimited field?
Junior Talker

Posts: 4
Name: Tim Mousel
Location: Houston, TX
Trades: 0
Hello,

Here's how my data is stored in the valid_host field:

www.review-script.com,review-script.com

I want to write a query that will select only up to the comma or in other words - www.review-script.com.

Currently I'm selecting the data (select DISTINCT valid_host, created from phpaudit_license) and then using php to get just the first address. I'd like to know how to do it using the select query.

Please advise.

Thanks,

Tim
Tim Mousel is offline
Reply With Quote
View Public Profile Visit Tim Mousel's homepage!
 
 
When You Register, These Ads Go Away!
Old 09-30-2009, 03:39 AM Re: MYSQL Select query from a comma delimited field?
tripy's Avatar
Do not try this at home!

Posts: 3,176
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Ok, again a quetion without specifying which database it is...

Taking a guess for mysql, the manual indicate that you can use an substring_index() function that fit nicely to your requirement:
http://dev.mysql.com/doc/refman/5.0/...ubstring-index

and it really does.
I used this script to test it:
Code:
drop table test1;

create table test1(
    valid_host varchar(255), 
    created timestamp
);

insert into test1 values ('www.review-script.com,review-script.com',now());

select substring_index(valid_host,',',1) as valid_host, created from test1;
returns
Code:
+-----------------------+---------------------+
| valid_host            | created             |
+-----------------------+---------------------+
| www.review-script.com | 2009-09-30 08:38:56 |
+-----------------------+---------------------+
1 row in set (0.00 sec)
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is online now
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 09-30-2009, 03:34 PM Re: MYSQL Select query from a comma delimited field?
Junior Talker

Posts: 4
Name: Tim Mousel
Location: Houston, TX
Trades: 0
Excellent. Thanks for taking the time!

Tim
Tim Mousel is offline
Reply With Quote
View Public Profile Visit Tim Mousel's homepage!
 
Reply     « Reply to MYSQL Select query from a comma delimited field?
 

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.17328 seconds with 13 queries