Solved

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

Posted on 2013-11-15
4
219 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 108

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Loading csv or delimited data files to MySQL database is a very common task frequently questioned about and almost every time LOAD DATA INFILE comes to the rescue. Here we will try to understand some of the very common scenarios for loading data …
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 …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now