count the number of results of mysql query with loop - mysql

I am trying to write a loop in mysql, so that the results of one query informs the second. This is my current query set:
select #post_date := from_unixtime(post_date)
from posts
where post_date > unix_timestamp('2012-10-20') and nsfw=1;
select #countofpost := count(#post_date);
while #countofpost > 0 DO
select count(*)
from live_sharedata.users
where joined between #post_date and (#post_date + 21600) and joined_site_id="RS";
set #countofpost = #countofpost -1;
end while;
The error I receive is [Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'while #countofpost > 0 DO.
Any thoughts would be greatly appreciated.

Two things:
Run SELECT #post_date; - does it show you the right information? Variables in MySQL cannot hold arrays, or multiple items. If you need the variables to hold multiple objects, you need to use CURSOR.
For your select #countofpost := count(#post_date); query, you could also re-write it as SET #countofpost = FOUND_ROWS();. FOUND_ROWS() returns the number of rows that would be returned were there no LIMIT clause, so there is no need to run the COUNT() command in the set of rows.
I believe, that you need to re-write your query using cursors, as (I think) that you expect the first SELECT #post_date := from_unixtime(post_date) ... query to return multiple objects. Please look at the Reference Manual's Cursor section for more information.

Related

How to declare MySQL valriable in pentaho-data-integration outside of the SQL query?

I have a dummy query like:
SET #cn1 = '';SET #rn1 = 1;SET #mdt = '';SET #scan = '';
SELECT
#rn1 := test value,
#cn1 := testvalue,
#mdt := testvalue
from dual;
In this query I've declared variables outside of MySQL which is not supported by pentaho ETL, as per my requirement I cannot declare these variables within the query as it is showing wrong rank for every result set.
Based on this scenario, how do I declare MySQL variables outside of the query in pentaho?
Thanks in advance.
You can use the Execute SQL script step along with Execute for each row AND variable substitution options.
The way it works is the question marks (?) in the script will be substituted for the Parameters listed in the step in the order they appear, and the Script will run N times, N will be the number of rows reaching the step.

MySQL Running Balance View Error

Hello I am trying to create a view in MySQL but I am getting syntax error # 1064 i.e
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for > the right syntax to use near 'SET #csum := 0; at line 3
Here how can I fix this variable issue any solution or alternate method to calculate running balance with ease
Here is my code
SET #csum := 0;
SELECT
tblleasesalesschedule_details.LeaseSaleID,
tblleasesalesschedule_details.ScheduleSr,
tblleasesalesschedule_details.InstallmentName,
tblleasesalesschedule_details.InstallmentSr,
tblleasesalesschedule_details.ScheduleDate,
tblleasesalesschedule_details.Amount,
IFNULL(tblleasesalespayment.Amount, 0) AS AmountPaid,
(coalesce(tblleasesalesschedule_details.Amount, 0) - coalesce(tblleasesalespayment.Amount, 0)) As BalanceAmount,
(#csum := #csum + (coalesce(tblleasesalesschedule_details.Amount, 0) - coalesce(tblleasesalespayment.Amount, 0))) as RunningBalance,
tblleasesalespayment.PaymentDate
FROM
tblleasesalespayment
RIGHT JOIN tblleasesalesschedule_details ON tblleasesalesschedule_details.InstallmentSr = tblleasesalespayment.InstallmentSr
WHERE
tblleasesalesschedule_details.PayDate < NOW();
You should avoi the use of var ins a view
A view definition is subject to the following restrictions:
(From Mysql Doc ) http://dev.mysql.com/doc/refman/5.6/en/create-view.html
The SELECT statement cannot contain a subquery in the FROM clause.
The SELECT statement cannot refer to system variables or user-defined
variables.
Within a stored program, the SELECT statement cannot refer to program
parameters or local variables.
The SELECT statement cannot refer to prepared statement parameters.
Any table or view referred to in the definition must exist. If, after
the view has been created, a table or view that the definition refers
to is dropped, use of the view results in an error. To check a view
definition for problems of this kind, use the CHECK TABLE statement.
The definition cannot refer to a TEMPORARY table, and you cannot
create a TEMPORARY view.
You cannot associate a trigger with a view.
Aliases for column names in the SELECT statement are checked against
the maximum column length of 64 characters (not the maximum alias
length of 256 characters).
if you need the use of param take a look at function or procedure http://dev.mysql.com/doc/refman/5.7/en/create-procedure.html

How to dynamicly assing LIMIT in MySQL?

I have query
SELECT *
LIMIT 3, (6 - 3)
Wchich returns:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(6 - 3)' at line 2
I basicly want to select 6 rows * from 3-rd row, in MSSQL it's WHERE RowNumber BETWEEN 3 and 6 but dynamicly knowing :from and :to parameters instaed of :from and :size
USE OFFSET in this case
SELECT name
FROM your_db
ORDER BY your_column DESC
LIMIT 10 OFFSET 10
The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must both be nonnegative integer constant.
Basically if you're not in a stored procedure or a prepared statement you can't do it.
Within prepared statements, LIMIT parameters can be specified using ? placeholder markers.
Within stored programs, LIMIT parameters can be specified using
integer-valued routine parameters or local variables
So, inside a stored procedure, the following would work:
declare from_row bigint;
declare to_row bigint;
SELECT name
FROM your_db
ORDER BY your_column DESC
LIMIT from_row OFFSET (to_row-from_row);

MySQL error 1064 (v 5.0.96) GROUP BY clause

I'm used to running on an Oracle database, so I'm not really quite sure how to trouble shoot this problem. I've narrowed down a simple example of my query to the following:
SELECT 0 as gm_rowID,
'-ALL Grantmakers-' as grantmakerName
FROM dual
GROUP BY 2
phpMyAdmin runs the SQL with the following error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER BY 2 LIMIT 0, 30' at line 1
Oracle can run this query just fine. MySQL can run the query without the GROUP BY clause. Any ideas?
--Here is the entire query:
SELECT
p.grantmaker_rowid as gm_rowID,
gm.grantmaker_companyName as grantmakerName
FROM grantmaker_info gm, proposal_submission p
WHERE 0=0
AND p.grantmaker_rowid = gm.grantmaker_rowid
UNION
SELECT
0 as gm_rowID,
'-ALL Grantmakers-' as grantmakerName
FROM dual
ORDER BY 2
GROUP BY 2
LIMIT 0 , 30
Columns selected for output can be referred to in ORDER BY and GROUP BY clauses using column
names, column aliases, or column positions. Column positions are
integers and begin with 1
From: http://dev.mysql.com/doc/refman/5.0/en/select.html
Unless you only have 1 column in that table, it should run fine. My suggestion however would be to reference the column name (or alias) of whatever you're trying to GROUP BY.
edit: My only other suggestion is to include the SHOW CREATE TABLE output for that table.
edit2: Ok I see you've updated your question. Why not instead of ORDER BY 2, you ORDER BY grantmakerName (if that's the column you want to order by?)

MySQL Is doing subquery after LIMIT syntax possible? If not, why?

I have MySQL Server version 5.1.53. I was looking for an hour to answer this question by myself. Including read the documentation itself at http://dev.mysql.com/doc/refman/5.1/en/select.html
Currently, I run this query.
SELECT dv2.timestamp
FROM data_val AS dv2
WHERE dv2.timestamp > '2011-06-10 22:26:25' ORDER BY dv3.timestamp DESC
LIMIT 1
Then I was trying to eliminate the ORDER BY syntax by determining the calculation of MAX_QUERIES minus 1. By doing that I could write,
SELECT (COUNT(*)-1) total
FROM data_val AS dv2a
WHERE dv2a.timestamp > '2011-06-10 22:26:13'
Finally the query becomes,
SELECT dv2.timestamp
FROM data_val AS dv2
WHERE dv2.timestamp > '2011-06-10 22:26:13'
LIMIT (
SELECT (COUNT(*)-1) total
FROM data_val AS dv2a
WHERE dv2a.timestamp > '2011-06-10 22:26:13'
), 1
And the error is:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '( SELECT (COUNT(*)-1) total FROM data_val AS dv2a ' at line 4
I also tried to put the subquery after OFFSET syntax. but still error.
Do you have any idea why my sub-query doesn't work?
I need technical details with short,
simple, and clean explanation.
From the MySQL manual: http://dev.mysql.com/doc/refman/5.5/en/select.html
The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must both be nonnegative integer constants, with these exceptions:
Within prepared statements, LIMIT parameters can be specified using ? placeholder markers.
Within stored programs, LIMIT parameters can be specified using integer-valued routine parameters or local variables as of MySQL 5.5.6.
The MySQL query optimizer needs to resolve the limit parameters to a constant before running the query, or it will not know how many rows to return.
You can't imbed a query result for a limit parameter