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
Related
Say I have a table with the structure
recordNumber: INTEGER (autoincrement)
insertedOn: DATETIME
Normally data gets inserted into the table it increments the recordNumber and insertedOn is always current time. Normally the following should be true
select insertedOn order by recordNumber === select insertedOn order by insertedOn
But that's actually not the case the question I have is how do I query the database so I can find the first recordNumber that would break the condition.
You can use LAG window function, but it depends on the particular database you are using.
If we assume that your increments are by '1', then this is a little more generic:
select top 1 *
from YrTbl Ths
where exists (select 1
from YrTbl Prev
where Prev.recordNumber+1=Ths.recordNumber
and Prev.insertedOn>=Ths.insertedOn
)
order by Ths.RecordNumber
TOP n might work a little differently in your environment; if you are using MySQL you might like to use LIMIT 1 at the end of the query, for example.
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';
i have an events table having start date and end date I am trying retrieve all the records by giving a date that is between start and end dates.
eg :
SELECT *
FROM `events`
WHERE '2017-01-29' BETWEEN start_date='2017-01-28'
AND end_date='2017-01-31'
but response is syntax error can any one help me to finish the query
Just list the columns.
WHERE '2017-01-29' BETWEEN start_date AND end_date
The values come from the table, you don't put them into the query.
According to mysql documentation (https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#operator_between) the syntax for BETWEN is
expr BETWEEN min AND max
it is not
expr BETWEEN blabla=min AND stuff=max
Also, it is rather pointless to be using constants in all three expressions, because in this case the result will be known in advance (either always TRUE or always FALSE) without having to consult the values in your table.
It is kind of hard to give you an example without knowing the structure of your table, but what you probably want is something like
WHERE '2017-01-29' BETWEEN start_date
AND end_date
(assuming start_date and end_date are columns in your table)
or something like
WHERE some_column BETWEEN '2017-01-28'
AND '2017-01-31'
(assuming some_column is a column in your table.)
I believe you're trying to find all the rows where a date is 2017-01-29, and so, your query could be:
SELECT *
FROM `events`
WHERE
date = '2017-01-29';
If, however, you want all rows with date between 2017-01-28 and 2017-01-31, then you could do:
SELECT *
FROM `events`
WHERE
date BETWEEN '2017-01-28' AND '2017-01-31';
Instead of putting 2017-01-29 before WHERE, put the name of the field you want to filter by date, such as EventDate (or whatever your field is named).
I am having hard time figuring out how I can select records if my (Between From AND To) is missing From.
Eg. On the form I have date range, and if user enters only TO and leaves FROM blank how can I select ALL records up to that point.
My issue occures here,
SELECT * FROM table WHERE date BETWEEN from AND to;
This is my query and I would like to use this same query and just modify my variables so that I don't have to have multiple SELECTS depending on what data was entered.
Thanks
I would suggest that you arrange your application to have two queries:
SELECT *
FROM table
WHERE date BETWEEN $from AND $to
and:
SELECT *
FROM table
WHERE date <= $to
Then choose the query based on whether or not $from is suppled.
Why? Both queries can take advantage of an index on date. In general, MySQL does a poor job of recognizing index usage with an or condition.
Alternatively, you can use AlexK's suggest and set $from to some ridiculously old date and use the query in the OP.
Try something like:
SELECT *
FROM table
WHERE ($from = '' AND date <= $to) OR
(date BETWEEN $from AND $to);
You can either do this with Giorgos' selection above, or using a union:
SELECT *
FROM table
WHERE date BETWEEN $from AND $to
UNION
SELECT *
FROM table
WHERE date <= $to AND $from IS NULL
Personally I think Giorgos' solution is a bit cleaner, but there are times you'll want a UNION so I'm including for completeness sake.
I have a mysql table contains lot's of records. my table has a varchar field and a timestamp field. (I have one record for every minute)
I want to select records like this:
1,3,5,7,9,11,...
or 1,4,7,10,13,..
or something like this.
I can get done it using php while function, but it is not a good solution. is there any mysql select parameter to get it exactly from mysql?
p.s: sorry for post title, this is the only title stackoverflow accept it.
select * from table where identity_column %2 <>0 -- to select 1,3,5,7,9...
and for your 2 condition do this !
select * from table where identity_column%3 =1 -- to select 1,4,7,10,13,....
For selecting records like 1,3,5,7,9,11,etc. You can do this:
SELECT *
FROM TableName
WHERE autoIncreamentField % 2
NB: Not necessary to check where clause against 0 or 1. It will select records if where clause returns 1. An example in Fiddle.
For records like 1,4,7,10,13,etc. You can do:
SELECT *
FROM TableName
WHERE (autoIncreamentField % 3)=1
select * from table order by rand()