0

My Table:

pno     name         lesson
-----------------------------
1       John         Math
1       John         Biology
2       Lisa         Biology
3       Emma         Math
3       Emma         Art
3       Emma         Pyhsic

You can think lesson like 'checkbox', when user checked the lesson, it will filter my table. Problem is here that I want to filter Only taking BOTH Math AND Biology!


When user checked only Math;

My Filtered Table:

pno     name         lesson
-----------------------------
1       John         Math
3       Emma         Math

When user checked Both Math and Biology;

My Filtered Table:

pno     name         lesson
-----------------------------
1       John         Math
1       John         Biology

My HTML Design is http://jsbin.com/adarih/2/

When user checked Both Math and Biology, i marked lime color. so Lisa have Biology but not Math. I'm not taking Lisa. Emma takes Math, but doesn't take Biology. I'm not taking Emma.

Actually it seem so easy. But it is crucial sql query for me...

4
  • Could you make it more clear please? Commented Feb 24, 2013 at 18:57
  • Your question is unclear. Perhaps showing some example code might help, along with expected and actual results. Commented Feb 24, 2013 at 19:02
  • 1
    i dont like when OP ask question and then dont answer those who want help him Commented Feb 24, 2013 at 19:14
  • sorry for late answer. it is clear for me... @mjshaw it is only query. when i solved the problem, i will right the code. Commented Feb 24, 2013 at 22:34

3 Answers 3

1

I'm not 100% certain what results you want to have returned, but you can get the distinct pno and name for each one that has both Math and Biology with a query like:

SELECT DISTINCT Table1.pno, Table1.name
FROM SomeTable Table1
  INNER JOIN SomeTable Table2 ON Table2.pno = Table1.pno AND Table2.lesson = 'Biology'
WHERE Table1.lesson = 'Math'
ORDER BY Table1.pno

For a more flexible query to support any number of matches, assuming you only ever have one of each lesson type (you may want to do a unique key on pno and lesson to guarantee this), you can do something like:

SELECT pno, name
FROM SomeTable
WHERE lesson IN ({Match List})
GROUP BY pno, name
HAVING COUNT(1) = {Total Number of options}
ORDER BY pno

Where the {Match List} is your list (such as 'Math', 'Biology' in your original question), and where {Total Number of options} is equal to the number of unique values that all have to match (2 in this case). Again, let me stress that this only works if each lesson per pno is unique... the moment it isn't, you'll get false positives.

Sign up to request clarification or add additional context in comments.

Comments

0

you may looking for this

$sql="select * from your_table 
where lesson =  '$biology'";
                      ^^-------this is the variable of which they select from check box

and when they select the other checkbox

you may add this

$sql .= "OR lesson= '$math' " ;

4 Comments

That won't work because lesson will never be both 'Biology' and 'Math' at the same time
Problem is here that I want to filter Only taking BOTH Math AND Biology!
That would be in two separate entries from the table!
First off, the OP does not specify PHP at all. Just MySQL. Secondly, a query of "select * from your_table where lesson = 'Biology' AND lesson = 'Math'" will ALWAYS return 0 results, because lesson cannot be both at the same time. I think you mean to have an OR in there, then in the PHP build the results in a data array and test against counts or the like to see that both are true?
0

If you want the list of people that take both Maths and Biology, this should work:

SELECT t1.pno, t1.name
FROM myTable t1
WHERE EXISTS
(
SELECT 1
FROM myTable t2
WHERE t1.pno = t2.pno
AND t2.lesson = 'Math'
)
AND EXISTS
(
SELECT 1
FROM myTable t3
WHERE t1.pno = t3.pno
AND t3.lesson = 'Biology'
)

1 Comment

it's working both Math and Biology. But user checked only Math, it is not working when i deleted last "and exists" block.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.