Solved

MySQL - need help with select statement

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

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 24

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

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

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 …
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 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