PHP - selecting multiple columns

Soldato
Joined
6 Mar 2008
Posts
10,079
Location
Stoke area
Hi all,

I have a table called quiz. In this quiz it has 6 fields:

Subject
tag1
tag2
tag3
tag
question
answer

I've got it to randomly pick X questions of a user defined type, as long as that type is in Subject or tag1-4. However, I need to display all of the subjects and tags removing duplicate entries.

If I use "SELECT DISTINCT Subject FROM quiz", it works.

However, I also need to add in the tag columns so figured it would be

"SELECT DISTINCT Subject, tag1, tag2, tag3, tag4 FROM quiz"

This doesn't show distinct entries and repeats some of them many times.

Is there a better way to do this?
 
What you're doing seems wrong to me. Care to expand slightly more? I'm thinking you want a group by, but I'm also thinking thats stupid and why would you have multiple entries if you were going to ignore them like that. I think your database design is probably suspect, but maybe I'm missing the obvious, I am fairly sleepy after all. :p
 
thanks kwerk, i'll try that when I get chance.

Aln, its basically a quiz site for our staff to use as part of their job. They enter the number of questions they want and then enter a subject and hit ok.

PHP pulls the info from the quiz table and will display however many questions they want on the set subject. The issue was that they didn't know what subjects were entered in to the quiz database.

PHP & SQL is new to me, as is database design so chances are I've set something up wrong.

Database consists of 1 table with which quiz questions get entered. Each question can have up to 5 tags, so for instance, a question on bears native to America could have the following :

Nature > wildlife > bears > america

Then I enter the questions and answer.

If I enter 100 similar questions they may have the same 'tags' on several of them. If I am pulling them all in to a div for staff to see I don't need 10 X America or 30 x wildlife entries to be pulled as it'll make it look even more cluttered.

I hope that explains it a little better :)
 
aln is right about the database looking suspect but unless you're expecting thousands of users I wouldn't worry about it (as long as it's secure). PHP is quick and dirty by its nature so for what it sounds like you're doing: if it works, it works.
 
Back
Top Bottom