Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PHP/MySQL – SELECT and SUM Distinct GROUP BY ColumnA WHERE columnB = ? AND Column C = ?. What is the best method for this?

Consider this table:

+-------------+--------------+-----------+------------+----------------+--------------------+
| Chapter(INT)| Unit (INT)   | Item (INT)| WordNo(INT)| Word (VCHAR)   |PartofSpeech(VCHAR) |
+-------------+--------------+-----------+------------+----------------+--------------------+
| 4           | 1            | 1         | 1          | He             | pronoun            |
| 4           | 1            | 1         | 2          | sells          | verb               |
| 4           | 1            | 1         | 3          | Apples         | noun               |
| 4           | 1            | 2         | 1          | Those          | pronoun            |
| 4           | 1            | 2         | 2          | apples         | noun               |
| 4           | 1            | 2         | 3          | are            | verb               |
| 4           | 1            | 2         | 4          | red            | adjective          |
| 4           | 1            | 3         | 1          | Green          | adjective          |
| 4           | 1            | 3         | 2          | grapes         | noun               |
| 4           | 1            | 3         | 3          | are            | verb               |
| 4           | 1            | 3         | 4          | delicious      | adjective          |
| 4           | 1            | 4         | 1          | These          | pronoun            |
| 4           | 1            | 4         | 2          | apples         | noun               |
| 4           | 1            | 4         | 3          | are            | verb               |
| 4           | 1            | 4         | 4          | rotten         | adjective          |
+------------------------------------------------------------------------------+

Now, say I want to be able to select each DISTINCT group of the ‘Item’ column WHERE Word = 'apple' AND PartofSpeech = 'adjective' and sum them. So based on this table, sum should be 2, and I should return the sentences for Item 2 ("Those apples are red.") and Item 4 ("These apples are rotten.").

I had assumed this query would work, but it seems I need to GROUP BY before I query, right?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

"SELECT SUM(itemnum) AS totalitems
FROM (SELECT COUNT(DISTINCT Item) AS itemnum 
FROM EnglishLevel1
WHERE Word = 'apples'
AND PartofSpeech = 'adjective'
GROUP BY Chapter, Unit, Item
) t"

I have also looked into the "IN" operator as well as changed AND to OR, but that doesn’t get the desired result. Perhaps a subquery is required?

>Solution :

You may use aggregation as follows:

SELECT Chapter, Unit, Item,
       GROUP_CONCAT(Word ORDER BY WordNo SEPARATOR ' ') AS sentence
FROM EnglishLevel1
GROUP BY Chapter, Unit, Item
HAVING SUM(Word = 'apple') > 0 AND SUM(PartofSpeech = 'adjective') > 0
ORDER BY Chapter, Unit, Item;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading