Solved

adding other users to an existing query: looking for the most simplified answer

Posted on 2013-11-15
4
224 Views
Last Modified: 2013-11-18
select
   case profile_id when 'bob' then 'bob evans' else substring(profile_id,1,3) end,
   case sender when 'bob' then 'bob evans' else substring(sender,1,3) end,
   message_id,
   case this_user when 'bob' then 'bob evans' else substring(this_user,1,3) end


select column1 from table1

if column1='bob', write 'bob evans'

select case column1 when 'bob' then 'bob evans' else column1 end as MyColumn
from table1


How to add this new case statement to this existing query
select substring(profile_id,1,3),substring(sender,1,3),message_id, substring(this_user,1,3)

where 'bob' can be profile_id, sender, this_user

NOTE: I want 'bob evans' else three letters of column name
Do not do substring if 'bob evans'



ADDING TO THIS QUESTION:

THERE ARE 5 OTHER USERS

'susan': 'susan jones'
'abe' : 'abe thomas'
'mary': 'mary davis'
'dave'; 'dave parker'
'joe': 'joe advo'
0
Comment
Question by:rgb192
  • 2
4 Comments
 
LVL 24

Expert Comment

by:mankowitz
ID: 39651999
First make a table for your common people:

create table people (
  abbrev varchar(5),
  fullname varchar(14)
  );

insert people (abbrev, fullname) VALUES
('susan', 'susan jones'),
('abe', 'abe thomas'),
('mary', 'mary davis'),
('dave', 'dave parker'),
('joe', 'joe advo')

Then, the query might look like this:

select
   coalesce((select fullname from people where abbrev=profile_id),
            substring(profile_id,1,3)),
   coalesce((select fullname from people where abbrev=sender),
            substring(sender_id,1,3)),
   message_id,
   coalesce((select fullname from people where abbrev=this_user),
            substring(this_user,1,3))
0
 
LVL 24

Accepted Solution

by:
mankowitz earned 250 total points
ID: 39652006
0
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 39653351
In database design there is the ACID concept, and the meaning can of "atomic" can be extended to the meanings for columns.  Each column should mean one thing and one thing only.  It's perfectly sensible to have the data base tables organized so that variants of the data are replicated.  I often do this with things that get misspelled a lot, like city names.  I keep both soundex() and metaphone() variants in the same row.  This lets me search for the city name as received from client input, but also search for similar-sounding rows.  If I miss on the name but match on the soundex or metaphone it lets me reply "Did you mean...?"

You might consider a design that had the three-letter search key abbreviation and the data you want to retrieve in separate columns:
 sKey   fName    fullName        responseData
'sus'  'susan'  'susan jones'   'sus'
'abe'  'abe'    'abe thomas'    'abe'
'mar'  'mary'   'mary davis'    'mar'
'bob'  'bob'    'robert evans'  'bob evans'

Open in new window

This will make your queries much easier to write, and if the data base ever grows to a large size it will scale up very smoothly.  The query would now say:
SELECT responseData FROM myTable WHERE sKey = $sKey LIMIT 1
0
 

Author Closing Comment

by:rgb192
ID: 39657034
Thanks for the sql code and the database acid tutorial
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to only show database items not chosen from initial captured multi select 11 51
simple mysql statement 3 39
mcrypt_create_iv() is deprecated 4 160
updating the date data 12 30
This guide whil teach how to setup live replication (database mirroring) on 2 servers for backup or other purposes. In our example situation we have this network schema (see atachment). We need to replicate EVERY executed SQL query on server 1 to…
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question