5
In a query in the database I inform the keywords, the month and the year. I want to get the data according to the keywords, the month and the year. For example: the word school on date 2015-08.
For this, I use the operator like
instead of =
because the date is complete and I want to do the search per month in a given year. The same thing goes for the keyword, because I may have other words than the word school on record.
The query is:
select * from despesas where palavraChave like "%escola%" or data like '2015-08%'
The mistake is:
If I pass no date and no word, it brings me the data and I don’t want it. I just want the data if one of the fields is found. I’ve used !null
, !notnull
, things like, but it doesn’t solve.
The language is sql and the server language is php.
What is database, Mysql, SQL Server, Oracle. Edit your question stating this. Another question: the date field is of what type?
– cantoni
Your date field is what type ? Varchar, Datetime ?
– Diego Souza
SELECT * FROM despesas WHERE palavraChave LIKE '%escola%' AND Month(Data) = 8 AND Year(Data) = 2015
– Diego Souza
Check whether variables have value or not before running the query.
– Felippe Tadeu