Storing day of week in PHPMyAdmin fields?
I've got a database called education1, and all content is stored in the table courses1
These are the fields within courses:
class VARCHAR(255),
classinfo VARCHAR(255),
dayofweek VARCHAR(255)
However, I want to store the day of week in the DATE FORMAT, had a look at http://dev.mysql.com/doc/refman/5.1/en/date-and-ti开发者_如何学JAVAme-functions.html#function_str-to-date but that was for queries.
I'm wondering how to store the day of the week the classes run as named days, Monday - Friday.
I tried ENUM, but it wouldn't let me use the dash symbol to display: Monday - Friday or Tuesday - Wednesday
You could do one of 2 things (note that those are recommended, you can do a whole bunch of other things.):
- Store an index of the day as
TINYINT
(0-6) or (1-7), and parse it on your PHP script to show as day names. Use an
ENUM
, for example:dayofweek ENUM('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday')
That goes in the
CREATE TABLE
query.
As mentioned above, there are other things you could do, these will probably be the most efficient and effective for your problem.
Do NOT use ENUM.
DO NOT use a varchar 255 field to hold a date or a string which is never longer than about 12 chars (depending on language).
If you want to hold a representation of a weekday in the database, then store it using a format you can use in a query to dereference a date value. MySQL has a function for this - DAYOFWEEK(date)
Then, if you must, maintain a table mapping these back to strings.
Just use SMALLINT(1) and thats all. As i remember week got 7 days. So: 1-7 values. And read about normalization. I think where Class, there should be class_id.
So, this should look like:
curses1
id | class_id | dayofweek
classes
id | name | info
Query: SELECT L.dayofweek, R.name, R.info FROM curses1 L LEFT JOIN clases R ON L.class_id = R.id
精彩评论