If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > retrieve id number from a string

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-27-10, 09:14
jx12345 jx12345 is offline
Registered User
 
Join Date: Apr 2010
Posts: 51
retrieve id number from a string

I need to retrieve a series of id number from a log table.

The log table has entries as follows:

log_id time event

1 2010-09-07 16:08:06 Quote sent to: customer_a [1176] cust_a@email.com

2 2010-09-07 16:08:06 Quote sent to: customer_b [1320] cust_b@email.com

The ids are in the square brackets and i need to retrieve a list of them (i know i should have designed my logs table better!)

I've come up with the following query:

select substr(`event`, locate('[', `event`)+1, locate(']', `event`) - (locate('[', `event`)+1)) as id from `logs` where substr(`event`,1,5) = 'Quote'

Which does work but seems a bit of a mess - i just wondered if there was a better solution?

any help/comments gratefully received

thanks

j
Reply With Quote
  #2 (permalink)  
Old 09-27-10, 09:28
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Code:
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(event,']',1),'[',-1)) as id 
  FROM logs 
 WHERE LEFT(event,5) = 'Quote'
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 09-27-10, 09:29
healdem healdem is online now
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,250
whats stopping you putting the ID as a separate column?
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #4 (permalink)  
Old 09-27-10, 10:05
jx12345 jx12345 is offline
Registered User
 
Join Date: Apr 2010
Posts: 51
thanks rudy, that looks more elegant...

healdem, nothing really stopping putting the ID as a separate column - only its just a general log table and records all sorts of stuff so there isnt always a customer ID. hadnt thought i'd have to use it like this originally,...
Reply With Quote
  #5 (permalink)  
Old 09-29-10, 09:16
jx12345 jx12345 is offline
Registered User
 
Join Date: Apr 2010
Posts: 51
sorry to bother you with this again...

I wont to join the result of this query back up with the details from my customer table and have attempted to do so with the following:

SELECT customer_id, name, last_contact_date, last_contact_date_unix last_order_date FROM customers WHERE customer_id IN
(SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(event,']',1), '[', -1) AS id
FROM logs
WHERE LEFT(event,5) = 'Quote')
AND
last_contact_date_unix < 1

however it just seems to hang the server - the first record returns about 2000 records and there's around 20000 in the customer table - is there a more efficient way of doing this? any ideas much appreciated.

ta

j
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

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