WHERE AND WHERE NOT in same query mysql - mysql

Is there a way to use WHERE NOT and WHERE in the same query, I am writing a python script where a function queries a mysql database, I need the script to call a specific name or have it return everyone, and on a side note if I can have it call groups of names that would be even better. I am accomplishing the name query with a simple LIKE statement.
SELECT * FROM db.table WHERE column LIKE '%searchterm';
However I want to be able to sort the data further using its DATE column, the query I have written for DATE is,
SELECT * FROM db.table WHERE NOT (column < startdate OR column > enddate);
Both queries work as intended when run on their own, when I try to combine them like so however I get a syntax error.
SELECT * FROM db.table WHERE NOT (column < startdate OR column > enddate) AND WHERE column LIKE '%searchterm';
Is there a way to fix the query so that it works?

You have one to many where in your query try changing to
SELECT * FROM db.table WHERE NOT (column < startdate OR column > enddate) AND column LIKE '%searchterm';

Related

Convert date in where clause MySQL

I'm looking to convert a date from 'd/m/Y' to 'Y/m/d' inside where clause like this:
SELECT * FROM myTable WHERE DATE_FORMAT(myDate,'%Y/%m/%d') = '2020/08/01'
this query always returns an empty result
There is a way to do it right ?
Thank you.
Simply convert you data to a date and then format it
SELECT * FROM myTable WHERE DATE_FORMAT(STR_TO_DATE(myDate,'%d/%m/%Y'),'%Y/%m/%d') = '2020/08/01'
Or make them both to dates, and then you can use Date fucntions
SELECT * FROM myTable WHERE STR_TO_DATE(myDate,'%d/%m/%Y') = STR_TO_DATE('2020/08/01','%Y/%m/%d')
The best is to save date as Date/Datetime/timestamp, so that you can with out problem, calculate with them and you always can convert them into any output

Mysql subquery or something better

I am somewhat new to mysql and I am having an issue on how I should best write the following query. Say I have a table that has a datetime column as well as a few others I want to search on. Since this is just one table, I don't think a join statement would be appropriate here (but I may be wrong since I have not done much in the way of join statements) and I think a subquery is what I need here. So my initial query is to search the table based on a search string the user entered and then I want to limit that on a datetime (start date and end date) also specified by the user in an HTML form.
Table Schema
id, datetime, host, level, message
I want to select any rows that contain $searchstring first so something like ...
SELECT * FROM $table WHERE (level LIKE '%$searchstring%') OR (message LIKE '%$searchstring%') LIMIT $offset,$limit
If I want to limit the above results also by the datetime column, the query would look something like this ...
SELECT * FROM $table WHERE (datetime >='$startdate') AND (datetime < '$enddate')
How can I best merge these queries into one so I can first get any rows that match the search query and then further limit the rows by the start and end datetime?
TIA
You can achieve that by using a single where condition.
In your case:
SELECT * FROM $table WHERE ((level LIKE '%$searchstring%') OR (message LIKE '%$searchstring%')) AND (datetime >='$startdate') AND (datetime < '$enddate') LIMIT $offset,$limit
You don't have to use a JOIN but only add a condition
SELECT *
FROM $table
WHERE (level LIKE '%$searchstring%' OR message LIKE '%$searchstring%')
AND
datetime >='$startdate'
AND datetime < '$enddate'
LIMIT $offset,$limit

Query to retrieve records containing Unix dates

One column in a table in my MySQL database stores date in Unix format.
I am trying to retrieve all rows where the date is later than a specific date. How do I write a query to do this? I want something like:
SELECT * FROM table_name WHERE date > "yyyy-mm-dd";
Thanks in advance.
use FROM_UNIXTIME
SELECT *
FROM table_name
WHERE DATE(FROM_UNIXTIME(date)) > "yyyy-mm-dd";

Float to date in mysql

We have a script that saves some rows in a table and one of them is filled with Time.now.to_f. So we have values like:
1330017921.1065
1330018520.80347
Is there a way to convert this to a date directly in MySQL I now I can use Time.at but I need a row mysql query.
i guess this is a microtime.
if you don't need milliseconds, you could query something like
SELECT * FROM table WHERE datefield = FROM_UNIXTIME(rounded_unixtime)
where rounded_unixtime would be in your case 1330017921

mysql query based on length of string in a column

I am trying to query a table in mysql based on the length of a string in a specific column. I know mysql has a function called LENGTH(), but that returns the length of the string. I want to be able to pull data based on the result of the LENGTH() function.
Example:
SELECT * table WHERE LENGTH(word) = 6
of course that does not work. I read through http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function%5Flength but could not find anything to help me.
yes I could make something in PhP to accomplish this, but I would like to do it at the query level.
Any help?
Try:
SELECT *
FROM table
WHERE LENGTH(RTRIM(word)) = 6
I believe you wanted to use query SELECT * FROM tableName WHERE LENGTH(word) = 6; (assuming that the word is name of column in tableName).
This is very unfortunate solution on large tables, you should create new column and use UPDATE tableName SET wordLength = LENGTH( word).