Solved

MySQL - need help with select statement

Posted on 2013-06-18
4
443 Views
Last Modified: 2013-06-18
$sql = "SELECT budget_type AS TYPE, budget_group_id AS ID, rec_description AS DESCRIPTION, rec_frequency AS FREQUENCY, rec_total AS TOTAL, rec_yearly_total AS 'YEARLY TOTAL' FROM `budget_planner` WHERE user_id='5028'";


The select statement returns budget_group_id values of 1-10, is it possible to display the following words instead of the values via the select statement, or should I create a separate table with these values and then join them?

 

1  Income
2  Commitments
3  Home
4  Utilities
5  Education
6  Health
7  Shopping
8  Transport
9  Entertainment
0
Comment
Question by:sabecs
[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
4 Comments
 
LVL 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 100 total points
ID: 39255451
Since they are not in the original table, creating one with the ids and names and joining it sounds like the right idea to me.  The alternative is to find a way to replace the id's with the names in the original table.
0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 350 total points
ID: 39255551
It's not entirely clear (to me) what you want exactly - it is possible - e.g.
SELECT
      budget_type AS TYPE
    , budget_group_id AS ID
    , case when budget_group_id = 1 then 'Income'
           when budget_group_id = 2 then 'Commitments'
           ... <<more like this>>
           else rec_description
      end AS DESCRIPTION
    , rec_frequency AS FREQUENCY
    , rec_total AS TOTAL
    , rec_yearly_total AS 'YEARLY TOTAL'
FROM `budget_planner`
WHERE user_id = '5028'

Open in new window

but you would need to code this whenever you need it again (& such a case expression could be applied to budget_group_id instead or as well),

If it's used often either a view containing this logic - or maybe another table would probably be preferable. But why don't these descriptions exist in the source table? (wouldn't that be the best approach?)
0
 
LVL 25

Assisted Solution

by:Tomas Helgi Johannsson
Tomas Helgi Johannsson earned 50 total points
ID: 39255658
Hi!

I agree with DaveBaldwin. Creating a separate table with the budgetgroup_id and the names is more practical if you want to add, delete or change the group names on later stages. Change the names in the table leaving the application code untouched.

Don't forget to create indexes on the budgetgroup_id columns in both tables to speed up the queries.

Regards,
    Tomas Helgi
0
 

Author Closing Comment

by:sabecs
ID: 39258137
Thanks for your input, much appreciated.
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
two ways encryption with php 3 51
Where on a calculated field 1 36
database connection error mysql stops 7 79
mysql qry 1 37
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

739 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