Welcome Guest, Not a member yet? Register   Sign In
select all the fields (exclude a few fields) from a table.
#1

[eluser]searain[/eluser]
In MySQL, say if I have twenty fields (cols) in a table, I want to select 18 of these 20 fields (cols), except two fields which I don't want to select.

I have to use "select field1, field2 ..." list all the 18 fields I want to select. I cannot just name the two fields that I don't want to select.

In CodeIgniter, is there a built in or 3rd party class(function) that I can use for the above purpose, naming the fields that I don't want to select, and then the rest fields will be selected?

Or use activerecords for this purpose?

Thanks!
#2

[eluser]troy_mccormick[/eluser]
Why wouldn't you want to just select those two extra columns by doing a select all on that database? Is two fields really going to cost that much processing time??

To answer the question though, I don't know of a way to do that, nor have I heard of it being done that way. This page seems to say the same: http://stackoverflow.com/questions/16289...y-in-mysql

Sorry...
#3

[eluser]searain[/eluser]
Not for the precess time reason, but for the "min to know" principle, say the table may have some fields only for internal usage.
#4

[eluser]jedd[/eluser]
[quote author="searain" date="1268277973"]In MySQL, say if I have twenty fields (cols) in a table, I want to select 18 of these 20 fields (cols), except two fields which I don't want to select.
[/quote]

My initial responses to this question are:

Your schema is likely badly designed - twenty columns suggests (but it's not definitive) that you are a long way from 3NF.

SELECT *'s are bad, and should be avoided. If you have to type in 18 fields once into your application, this doesn't seem hugely onerous. Use the mouse if keyboard shortcuts elude you.

Okay - that second one was a bit too sarcastic. I blame the sherry. (Yes. I'm old.)
#5

[eluser]searain[/eluser]
20 fields is just a fictional number, I do have a few tables have 20 fields, but most of them are less. It just naturally has these fields in and I don't have too many records to deal with. From hundreds records to no more than 2,000 records each table etc.

The reason that I try to exclude a few fields for the select *, is not for the typing reason either (not for the performance reason too Smile)

But more likely for a framework I am designing. It is based on CodeIgniter. I have my framework's database set up, CRUD is done etc. And if a new field added, I just need add a new field in that table's structure in the database. The rest CRUD php scripts etc are all taken care of. I don't have to go to my system to change any CRUD script. The new added fields will be automatically show in the system.

But if I have to name each field I want to select, then I have to go to my system and recode some CRUD script, when I add a new field to or remove a field from a table.
#6

[eluser]searain[/eluser]
I guess I can just elimate these fields from the view page and be done with it. Instead of trying to do it in the model etc. This is a view issue not a database issue anyway.
#7

[eluser]jedd[/eluser]
[quote author="searain" date="1268288968"]
This is a view issue not a database issue anyway.
[/quote]

SELECT *‘s are bad, and should be avoided.

Your schema is likely badly designed.




Theme © iAndrew 2016 - Forum software by © MyBB