开发者

Working with MySQL Vertical Designed table

I have an html form (each form has different page, according to date), with five text inputs, called date, inp1, inp2, inp3 and inp4.

For example, I have database table, with structure like this: id | parent | name | value. In this case, I want to store my data, and adding it to date by parent, like this:

id | parent | name | value
1  | 0      | date | 20.07.2011
2  | 1      | inp1 | value-from-开发者_StackOverflow中文版inp1
3  | 1      | inp2 | value-from-inp2
4  | 0      | date | 21.07.2011
5  | 4      | inp2 | value-from-inp2
6  | 1      | inp3 | value-from-inp3
7  | 4      | inp1 | value-from-inp1

and so on...

But problem starts here. I want to create option, that you can update those values for previously added. But the QUESTIONS are, how to check, if

  1. Do I have anything assigned to that date already?
    1. If is assigned, how to determine, and use MySQL Update for that
    2. If is not assigned, how to determine, and use MySQL Insert Into for that

My stupid and 50% more inefficient of loading page solution for this problem sounds like this (Don't worry about security, its just mock-up):

if($_POST['submit']){
$inp1_data = $db->get_row("SELECT name FROM table WHERE parent = ".$parent_id." and name='inp1'");
if($inp1_data){$db->update($query)}else{$db->insert($query)}

// ... and so on, four copies of same code, just replacing "inp1" to "inp2", "inp3"...
}

Yes, I know, this is useless script, in case, if I have added one more, or hundreds of inpNUMBER, it would be crush test site, 100% guaranteed timeout, not working script.


It sounds to me like you're trying to use a singe table where you really need two. To answer your question, because the date is stored separately from the value, there is no way to find out if you have a value assigned to a particular question using a single SELECT. You may be able to do this with a nested select, but I won't try to go through the contortions to make it happen.

Consider this alternative design, which assumes that a given field can't be repeated for a given submission:

Submission table

submission_id | date
1             | 20.07.2011
2             | 21.07.2011

answers table

submission_id | field_name | field_value
1             | inp1       | value-from-inp1
1             | inp2       | value-from-inp2
2             | inp2       | value-from-inp2
2             | inp4       | value-from-inp4
1             | inp3       | value-from-inp3

Note that Randolf Rincón-Fadul's suggestion of keeping the data types separated by field is a good one, but I haven't done it here to keep things simple. Now you can find out if you have an answer for a particular date by joining the tables.


RobertR I agree with Kerrek this is a VERY BAD idea man! You should normalize your data. This structure doesn't provide any of the real DB advantages and trying to parse and maintain data based on your "schema" will be painful.

Theres is always a form. If you need to maintain user defined data then use generic db columns like [FIELD_NAME][FIELD_TEXT_VALUE] and [FIELD_NAME][FIELD_DATE_VALUE] where all are atomic.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜