Solved

MySQL - need help with select statement

Posted on 2013-06-18
4
440 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
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MySQL Select latest stock count records 11 56
How to connect to a remote mysql server using C#? 9 56
MySQL: Updating SubQuery Match Faster 9 46
update joined tables 2 31
Introduction In this article, I will by showing a nice little trick for MySQL similar to that of my previous EE Article for SQLite (http://www.sqlite.org/), A SQLite Tidbit: Quick Numbers Table Generation (http://www.experts-exchange.com/A_3570.htm…
I have been using r1soft Continuous Data Protection (http://www.r1soft.com/linux-cdp/) for many years now with the mySQL Addon and wanted to share a trick I have used several times. For those of us that don't have the luxury of using all transact…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

911 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

23 Experts available now in Live!

Get 1:1 Help Now