M
M
MdaUZH2016-01-24 21:10:49
PHP
MdaUZH, 2016-01-24 21:10:49

COUNT(*) in SQL along with GROUP BY, unexpected result?

Hello.
A little hitch came
out
id | text | parent
1 | text | 2
2 | text | 2
3 | text | 3
.
to make sure that parent is not repeated?
I tried GROUP BY parent, but then the problem arose with COUNT (*),
when I do COUNT (*) with GROUP BY,
the result is this:

COUNT(*)
2
3
2
2
2
1
2
2
2
2
2
2
2
2
2
4

Please tell me how to make sure that a certain field does not repeat (combine fields), but at the same time not break COUNT(*) ?
UPD:
There are 3 tables:
Recipes: Recipe
id | name | description
components:
id | parent_id | recept_id
where: parent_id -> component id (I don’t use a separate table here)
recept_id -> id of the recipe where this component is used
Components that the user owns:
id | user_id | parent_id
where: user_id -> user id whose component, parent_id -> component id (a separate table here without it)
Task:
Having only the user ID, find all the recipes that he can cook (based on his components)
I do this:
SELECT r.*, cr.*, uc.*
FROM user_component AS uc
JOIN component_recept AS cr ON cr.parent_id = uc.parent_id
JOIN recept AS r ON r.id = cr.recept_id
WHERE uc.user_id
IN ( 9 )

And everything is fine, but the only problem is that as a result there are many "identical" cells...
how to merge the selection so that there are no identical recipe ids?
I tried GROUP BY, but the results and the problem are slightly higher, thanks

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Logvinenko, 2016-01-24
@MdaUZH

The task is not clear, and you would have attached a request ...
In general, if you need to display parent's and the number of rows for them, then like this:

SELECT parent, COUNT(1)
FROM tbl
GROUP BY parent
.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question