Error Transporting a View From Oracle to MySQL DB - mysql

I'm migrating somebody else's View from an Oracle DB to a Mysql DB. I'm not familiar with the table(cast(multiset())) operator and so I have no clue how to transpose it to MySql
The part of the code that gives me an error is the following:
SELECT csCOUNTRY.ID, csCOUNTRY.COUNTRY_ID,
trim(regexp_substr(COUNTRY_ID, '[^;]+', 1, levels.column_value)) AS COUNTRY_ID_2
FROM table_study csCOUNTRY,
table(
cast(
multiset(select level from dual connect by level <= length (REGEXP_REPLACE(COUNTRY_ID, '[^;]+')) + 1) as sys.OdciNumberList)
)
levels)
The error I recive 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 'table(cast(multiset(select level from dual connect by level <=
length (REGEXP_R' at line 255
How can I transpose it correctly?

Multicast converts rows into a single table-type. Oracle table's column type can be standard Oracle SQL Types(number, char, varchar2) or user defined table types.
But, here the cast and multicast operator is redundant.
Below query should give same result :
SELECT csCOUNTRY.ID, csCOUNTRY.COUNTRY_ID,
trim(regexp_substr(COUNTRY_ID, '[^;]+', 1, levels.lvl)) AS COUNTRY_ID_2
FROM table_study csCOUNTRY,
(select level lvl
from dual
connect by level <= length (REGEXP_REPLACE(COUNTRY_ID, '[^;]+'))) levels

Related

when i fetch the data from database it give error on sql command . how can i fixed the code

SELECT (SUM(MAX(identifier)+1) FROM student_ids WHERE state ='state' AND year='year');
SELECT (SUM(MAX(identifier)+1) FROM student_ids WHERE state ='state' AND year='year')
LIMIT 0, 25
MySQL said: Documentation
#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 'FROM student_ids WHERE state ='state' AND year='year')
LIMIT 0, 25' at line 1
how can i fix the error
The SUM function is for taking aggregates of columns, across multiple rows. You don't need to use it to add a scalar value:
SELECT MAX(identifier) + 1 AS max_id
FROM student_ids
WHERE state = 'state' AND year = 'year';
Additionnaly to #tim-biegeleisen, use ` for column names and table names, like this : `year`, `state`, ...
Because some words you uses are reserved. YEAR is reserved word and could return an error if you uses it as column name. SQL thinks you are calling the year function instead of a column named year.
SELECT MAX(`identifier`) + 1 AS `max_id`
FROM `student_ids`
WHERE `state` = 'state'
AND `year` = 'year' ;
Here is documentation about reserved words in mySQL :
https://dev.mysql.com/doc/refman/5.5/en/keywords.html

Why does this strcmp() syntax give me an error in mysql-5.7?

I was trying to test out the strcmp() function with strings
containing random email addresses in this SQL statement:
INSERT IGNORE INTO possible_duplicate_email
-> (human_id, email_address_1, email_address_2, entry_date)
-> VALUES(LAST_INSERT_ID(), 'bobyfischer#mymail.com',
'bobbyfischer#mymail.com')
-> WHERE ABS( STRCMP('bobbyrobin#mymail.com', 'bobyrobin#mymail.com') ) = 1;
Then I got this error message:
ERROR 1064 (42000): 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 'WHERE ABS( STRCMP('bobbyrobin#mymail.com',
'bobyrobin#mymail.com') ) = 1' at line 4
I've read the strcmp() documentation from the mysql-5.7 reference manual
and tried a simple SELECT statement to see that strcmp() returned a numeric
value ranging -1, 0, 1, which it did, and I've included the IGNORE option
to the SQL statement so as to proceed through errors. Could someone explain to me why I get this error when running strcmp() in the WHERE clause?
Thanks to Paul T., the answer is:
INSERT statements do not have a WHERE clause, unless doing an INSERT ... SELECT statement, then the SELECT portion of the query can use a WHERE clause.

MySql: Using an alias in IF/IFNULL

I found this post:
Using an Alias in SQL Calculations
which suggests you can use an alias in calculations by using
(select alias)
like:
SELECT 10 AS my_num,
(SELECT my_num) * 5 AS another_number
FROM table
This works fine. But now I want to use an alias in an if. So I thought it might work the same way. So I tried:
SELECT 10 AS my_num,
IFNULL(otherfield, (SELECT my_num)) AS another_number
FROM table
which doesn't work at all, telling me
ERROR 1064 (42000): 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 my_num)) AS another_number
Is there any way to make this work in MySql?
No, use the entire expression directly like below unless you are accessing it in a outer query.
IFNULL(otherfield, 10) AS another_number
In your case it should be
SELECT 10 AS my_num,
IFNULL(otherfield, 10) AS another_number
FROM table

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

MySQL SELECT with fields less and greater than values

I am trying to do a select query on MySQL with phpMyAdmin or PHP with PDO.
SELECT 'uid' FROM 'clusters' WHERE 'lat'<='47.21125' AND 'lat'>='39.21125' AND 'lng'<='32.90243' AND 'lng'>='22.90243'
However, phpMyAdmin says:
#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 ''clusters' WHERE 'lat'<='47.21125' AND 'lat'>='39.21125' AND 'lng'<='32.90243' A' at line 1
What is wrong with it?
'' creates a string literal in MySQL, so your query is selecting the literal "uid" from the literal "clusters," which is invalid. Use backtics (or nothing)
SELECT Uid FROM clusters WHERE lat <= 47.21125 AND lat >= 39.21125
AND lng >= 22.90243