Pass a php array in a query and return multiple results

Asked

Viewed 287 times

1

I want to pass a dynamic POST array of type:

array(2) { [0]=> string(1) "1" [1]=> string(1) "3" }

For an SQL query where it returns multiple results depending on the string values in the array

Select nome, count(avaliacao) FROM tabela WHERE '1'
Select nome, count(avaliacao) FROM tabela WHERE '2'

And the result of the query will be:

Array(2) ( [result 1] => 10, [result 2] => 13 )

I’ve tried with the implode and SQL IN but returns only the total of count of 1 and 3.

  • 1

    You should think of a writing logic of the sql line, to concatenate all the values of the array

  • 2
  • @Francisco, what I want is a little different. I want a query for each result... I have already used SQL IN and the result is Count 1 and 3, not Count 1 and Count 3.

  • It’s very poorly explained, I can’t understand what you want... Explain why you want to do this.

  • @Francisco I want to build graph from the multiple information selected in the form I have behind.

  • The IN seems to be quite feasible for you. If you are in doubt, see that example.

  • Also I can only see the in as a solution, then I think it will be better if you edit the question and add the table structure, examples of records present in it and what the expected result would be. As it stands, the question is not clear enough.

  • Also try to group the query by the relevant fields. Ex: Select nome, count(avaliacao) FROM tabela WHERE id IN (1,2,5,10) GROUP BY nome

  • @rray his answer, already changed the panorama and showed almost an approximation of the expected result. It lacks the hierarchy of grandchildren until the grandmother. Because in IN is a variable that various values and I want to return the value of them. The problem is that a table has the definition of the hierarchy of the grandmother up to the children and grandchildren. And on the other you have the count for the final result for the corresponding id. This id is the id corresponding to the one entered in db.

Show 4 more comments

1 answer

2

I believe you should change the query to perform only one query and bring all the results you need.

Another detail is missing the name of the column there on Where, but I will consider that you forgot to put only here in the question. Try this:

SELECT 
    nome, count(avaliacao) 
FROM 
    tabela 
WHERE 
    nomeColuna in('1','2') 
GROUP BY 
    nome;
  • Now I have a problem. query = "SELECT a.menuName the name, COUNT(DISTINCT b.id_rating) as THEME FROM menu a INNER JOIN answers b ON (a. menuId = b. menuId) WHERE b.menuId IN (SELECT m3.menuId from menu m3 WHERE m3.menuIdPai in (SELECT m2.menuId from menu m2 WHERE m2.menuIdPai in (select m3.menuId FROM menu m3 WHERE m3.menuId IN ('". implode("','",$id)." '))) GROUP by menuNome"; This returns for each child the number of occurrences. In which $id is the number of occurrences of avo(root).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.