Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-11-15
4
Medium Priority
?
231 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1000 total points
ID: 39652006
0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

719 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