Mysql Error: Each derived table must have its own alias

Asked

Viewed 641 times

1

I’m trying to put two tables together questionario and resposta and select from the table questionario two dates, this was the code I tried in php:

$verificar=mysqli_query($link,"SELECT questionario.*, resposta.* from questionario 
JOIN resposta on questionario.pergunta_id=resposta.pergunta_id,
(select COUNT(pergunta_id) as total, classificacao from questionario 
where data BETWEEN '".$_POST["datainicial"]."' AND '".$_POST["datafinal"]."' 
group by classificacao");
        while ($linha = mysqli_fetch_assoc($verificar)) {...}

But it returns this error:

Warning: mysqli_fetch_assoc() expects Parameter 1 to be mysqli_result, Boolean Given

I’ve reviewed the Internet, and I can’t find any solution.

So I tried the following on phpMyAdmin:

SELECT questionario.*, resposta.* from questionario 
JOIN resposta on questionario.pergunta_id=resposta.pergunta_id,
     (select COUNT(pergunta_id) AS total, classificacao from questionario where data 
BETWEEN '2018/06/07' AND '2018/06/08' GROUP by classificacao)

As expected the query did not work, it returns this error

Each derived table must have its own alias...

Here is the structure of the tables
inserir a descrição da imagem aqui


Examples of some data inserted in tables

Table inquiry
inserir a descrição da imagem aqui

Response table
inserir a descrição da imagem aqui

  • Do you have the structure of the tables? If you can post them it is better to help.

  • I’ve already altered Clayton!

  • Hi Ana! I’m not sure what you’re looking for, Group of questions by rating, along with the whole inquiry and question? What columns do you want exactly? the missing alias is due to Count’s select, but I need to figure out what you want to solve ;)

  • Good morning Ana, yes what I intend is to join both tables, return all information, between two dates. The query in phpMyAdmin already works, it returns what I intend, but in php appears the following error Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in

1 answer

1


Ana, use this query below:

Select COUNT(questionario.pergunta_id) total,questionario.*, resposta.* from 
questionario 
JOIN resposta on questionario.pergunta_id = resposta.pergunta_id
Where questionario.data BETWEEN '2018-06-07' AND '2018-06-08'
group by questionario.pergunta_id

Answer to PHP error: Mysql error "expects Parameter 1 to be Resource, Boolean Given in"

  • Clayton, thank you the query works perfectly in phpMyAdmin! But I need it to work in php, and I need a count to receive the total of classificacao as shown in the question table (in the structure)..

  • The return in PHP using the query I posted still error or does it work? You can post some sample records of the two tables so I can do some tests?

  • 1

    I’ve changed Clayton again! Your PHP query gave error, because following my code, I need the "total" that was supposed to be in (select COUNT(pergunta_id) as total, classificacao...

  • I changed the query to contain the total number of answers per question Ana. Regarding the error in PHP if it persists, I put a link to a question already answered with this error

  • Clayton, it worked your query, and I went to see the answer you recommended, and in fact my error was in "reply" I typed wrong!! Thank you very much!

  • How nice Ana! For nothing

Show 1 more comment

Browser other questions tagged

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