Trying to combine INSERT and SELECT queries in MYSQL DB - mysql

I have been trying to combine SELECT and INSERT queries, but with no luck. The below query works well..
INSERT INTO counts (count)
SELECT COUNT(*) FROM `twitter` WHERE created_local > NOW() - INTERVAL 10 MINUTE AND text LIKE '%USDJPY%'
My DB columns that I do INSERT query are "pair" and "count".
In above query the count is inserted into the counts table > count column but I am also trying to insert in the above case "USDJPY", tried different combinations but all end up with sql error.
I would appriciate a feedback...

Just select a constant for the literal value you want to insert. And also, please specify all column names which are being targeted by your insert.
INSERT INTO counts (pair, count)
SELECT 'USDJPY', COUNT(*)
FROM twitter
WHERE created_local > NOW() - INTERVAL 10 MINUTE AND text LIKE '%USDJPY%';

Related

How to get a constant value back in SQL

I have a SQL query that fetches 50million records .
I want to assign a constant value to each row in the results which should be different everytime I run the query.
So I decided to use system date in the query.
My query looks like :
SELECT (SYSDATE,'DD-MM-YYYY - HH:MM:SS'), column1,column2,column3
FROM my_table
But by the time query execution ends I get different values of HH:MM:SS in the sysdate column.
Is there a way I can make this constant?
Currently My Results Look Like:
SYSDATE
01-03-2019 01:20:55
01-03-2019 01:20:56
01-03-2019 01:20:57
01-03-2019 01:20:58
01-03-2019 01:20:59
This is because there are so many records the database takes time to select all of them.
You can create a variable to hold the sysdate before you start the query and select it along with your columns:
SET #Date = (SYSDATE,'DD-MM-YYYY - HH:MM:SS');
SELECT #Date, column1,column2,column3
FROM my_table;

sql error using between operator with date between two dates

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).

Using date separated colums to compare in mysql

I have three columns in a table to save a date, but now I need to compare those three columns as a date using the CONCAT and STR_TO_DATE function, but the comparision does not work, I don't see the error.
Query
SELECT * FROM mytable WHERE STR_TO_DATE(CONCAT(myyear,'-',mymonth,'-',myday),'%Y-%m-%d')>'2015-01-01'
For your example, why not just use this?
select *
from mytable
where myyear >= 2015

I have a mysql table contains lot's of records

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()

Returning the closest to a date in a table using PDO Mysql/MSSQL

I have 2 PDO database connections. I am doing a search within a MS SQL table for a row that closest matches a date (mysql datetime) row.
I have mysql.table1.date passed to mssql.table and I am looking for the closest date accordingt to the mssql.table.date. It is also defined as a datetime field. I only need 1 row returned, the closest to the time, so in essence:
SELECT * FROM table ORDER BY CLOSEST(mysqldate = mssql.table.date) LIMIT 1;
I know the syntax above is incorrect but that basically outputs what I need, but I really do not know how to do this with mssql.
Any help?
Basically u can find the difference of the mysql date with all the dates in mssql.Table.Date column .Then u need to select the least difference value from the above query .Hopefully the below query might help u
;with CTE as
(
Select mssql.table.date,row_number()
over (order by abs(datediff(day,mysqlDate,mssql.table.date))) rowNumber
from mssql.Table)
select mssql.table.date from CTE where rowNumber=1
A simple solution which worked for me was to do the following:
SELECT * FROM `table` WHERE `date` < `startDate` ORDER BY `date` LIMIT 1;
This returns 1 row matching the closest time to the time I am passing :)