MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMySQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 7th, 2012, 08:54 AM
andaluzo andaluzo is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 4 andaluzo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 52 m 7 sec
Reputation Power: 0
SQL Question

Hi,

I want to count a string in different fields in one row of a table.

Table

id, field_1,field2, field3 ..... field_63,field_sum_A,field_sum_B,field_sum_C


with the ID I select which row i want to count.

In the fields (field_1 to field_63) is a string (A,B or C) and I want to count
how many A how many B AND how many C and update the fields field_sum_A, field_sum_B, field_sum_C.

After searching and trying for hours I ask here for help

Thanks

Reply With Quote
  #2  
Old December 7th, 2012, 09:29 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,357 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 4 h 40 m 42 sec
Reputation Power: 4140
any time you find yourself "going sideways" across a single row, doing something to each column one after the other, it's a sign that the table is poorly designed

having 63 identical columns practically screams this point

do a search on first normal form and you will learn that instead of 63 similar columns, you'll want a separate table where there are 63 rows instead

then your query to count stuff becomes trivial
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Reply With Quote
  #3  
Old December 7th, 2012, 10:26 AM
andaluzo andaluzo is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 4 andaluzo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 52 m 7 sec
Reputation Power: 0
Design ?

I agree, as I do not design tables on a daily base, there sure can be a lack of understanding.

The data comes from a questionary where these 63 questions are written to the table for each user one row with the 63 questions (A,B,C)

If you can give me an hint how to design that, would be very helpful.

Thanks

Reply With Quote
  #4  
Old December 7th, 2012, 11:14 AM
SimonJM SimonJM is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Mar 2006
Posts: 2,107 SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 1 Day 3 h 57 m 31 sec
Reputation Power: 1485
In this instance you'd have three table:
1) User, with a unique id to identify them
2) Question, with a unique id, to identify each
3) User_Question, no real need for a unique, single key, but a joined unique key based on User id and Question id. This table would contain the value A, B or C.
__________________
The moon on the one hand, the dawn on the other:
The moon is my sister, the dawn is my brother.
The moon on my left and the dawn on my right.
My brother, good morning: my sister, good night.
-- Hilaire Belloc

Reply With Quote
  #5  
Old December 7th, 2012, 12:18 PM
andaluzo andaluzo is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 4 andaluzo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 52 m 7 sec
Reputation Power: 0
Thank you for your hint.

just one more question.

the questions are mainly radio inputs

where I have in the form f.ex

Code:
<INPUT type="radio" name="Question" value="A"> Answer A<BR>
<INPUT type="radio" name="Question" value="B"> Anser B<BR>


So how I would do then the combination between that 2 Tables (Question and User_question)

Where I will have

Table Question

Question_id
Question

and

Table User_question
User_Id
Question_id
Answer




Quote:
Originally Posted by SimonJM
In this instance you'd have three table:
1) User, with a unique id to identify them
2) Question, with a unique id, to identify each
3) User_Question, no real need for a unique, single key, but a joined unique key based on User id and Question id. This table would contain the value A, B or C.

Reply With Quote
  #6  
Old December 8th, 2012, 12:12 PM
SimonJM SimonJM is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Mar 2006
Posts: 2,107 SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 1 Day 3 h 57 m 31 sec
Reputation Power: 1485
You will, I presume know the identity of the user, which will give you the suer id part of the equation. From what you show I am not sure how you identify the question, but on the assumption you can (it'd be what you'd link with the field_1, field_2 parts) that gives you the other main part of the key. After that it's just down to what radio button is selected to give the value you'd insert into the User_Question table.

Reply With Quote
  #7  
Old December 8th, 2012, 12:19 PM
andaluzo andaluzo is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 4 andaluzo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 52 m 7 sec
Reputation Power: 0
Thanks, got it !

Quote:
Originally Posted by SimonJM
You will, I presume know the identity of the user, which will give you the suer id part of the equation. From what you show I am not sure how you identify the question, but on the assumption you can (it'd be what you'd link with the field_1, field_2 parts) that gives you the other main part of the key. After that it's just down to what radio button is selected to give the value you'd insert into the User_Question table.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > SQL Question

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap