Can't create a MySQL View - mysql

I run this SQL code to create a view but this doesn't work :
Create VIEW as
select
c.ID_CAPTEUR as ID_CAPTEUR,
d.ID_CAPTURE as ID_CAPTURE,
d.VALEUR_CAPTURE as valeur,
d.DATE_CAPTURE,
t.INTITULE_TYPE_CAPTURE as TYPE_CAPTURE,
z.INTITULE_ZONE as zone,
r.INTITULE_REGION as region
from CAPTEUR c, CAPTURE d, TYPECAPTURE t, ZONE z, REGION r
where c.ID_CAPTEUR=d.ID_CAPTEUR
and d.ID_TYPE_CAPTURE=t.ID_TYPE_CAPTURE
and c.ID_ZONE = z.ID_ZONE
and z.ID_REGION = r.ID_REGION
I get this 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 'as select c.ID_CAPTEUR as ID_CAPTEUR , d.ID_CAPTURE as ID_CAPTURE , d.VALEUR_CAP' at line 1

This is the syntax of the CREATE VIEW statement:
CREATE
[OR REPLACE]
[ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}]
[DEFINER = { user | CURRENT_USER }]
[SQL SECURITY { DEFINER | INVOKER }]
VIEW view_name [(column_list)]
AS select_statement
[WITH [CASCADED | LOCAL] CHECK OPTION]
As you can see, your query is missing the view_name parameter (parameters between [ and ] are optionnal, which is not the case of view_name).

Related

Loop php array stored in one SQL cell

I have a table which contains body of xml document (like php array):
a:26:{s:4:"HEAD";a:1:{i:0;a:8:{s:5:"BUYER";s:13:"4640014349991";a:5:{s:7:"varGUID";s:3:" ";}s:8:"POSITION";a:17:{i:0;a:14:{s:14:"POSITIONNUMBER";s:1:"1";s:11:"DESCRIPTION";s:67:"Value 0";s:9:"UNITPRICE";s:5:"11.40";}i:1;a:14:{s:14:"POSITIONNUMBER";s:1:"2";s:11:"DESCRIPTION";s:69:"Value 1";s:9:"UNITPRICE";}i:2;}}s:7:"ECONINF";s:46:"Text";}
One cell for one document.
Now I need to found for example all of UNITPRICE which is duplicating, so I should iterate this SQL cell to find all of UNITPRICE. How I can do this?
I am trying to use SUBSTRING_INDEX for this, but it is not working because I think that substring will be show only first value of UNITPRICE in cell:
SELECT d.varName AS "Name 1",
c.varName AS "Name 2",
a.varDocNum AS "Number",
SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(b.`xmlBody`, '"PRODUCTIDBUYER";',-1),'"',2),'"',-1) AS "PRODUCTIDBUYER",
SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(b.`xmlBody`, '"DESCRIPTION";',-1),'"',2),'"',-1) AS "DESCRIPTION",
SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(b.`xmlBody`, '"UNITPRICE";',-1),'"',2),'"',-1) AS "UNITPRICE"
FROM xmls AS a
LEFT JOIN xmls_body AS b ON a.intDocID = b.intDocID
LEFT JOIN adresses AS c ON SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(b.`xmlBody`, '"BUYER";',-1),'"',2),'"',-1)=c.varAdress
LEFT JOIN adresses AS d ON a.varSender=d.varAdress
WHERE a.varRecipient in ('4640014349991')
AND a.intSendTimestamp < UNIX_TIMESTAMP('2020-02-09 23:59:59')
AND a.intSendTimestamp > UNIX_TIMESTAMP('2019-12-09 00:00:00')
AND SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(b.`xmlBody`,'"PRODUCTIDBUYER";',-1),'"',2),'"',-1) IN(
'110324',
'103208'
)
AND c.varName LIKE '%1065%'
OR '%1008%'
UPD:
I've create procedures like:
DELIMITER |
CREATE PROCEDURE prcdProduct()
BEGIN
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(q.`xmlBody`, '"DESCRIPTION";',-1),'"',2),'"',-1) AS "Product" FROM xmls_body AS q;
END
|
DELIMITER ;
but I am getting 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 'CALL prcdProduct(),
when I am trying to use stored procedure:
SELECT d.varName AS "Supplier",
c.varName AS "Shop",
a.varDocNum AS "УПД",
CALL prcdProduct(),
...
if I delete CALL I will have error that FUNCTION dbName.prcdProduct does not exist. But this I think because of without CALL we are executing functions, not PROCEDURE

Error Code: 1060 Duplicate column name 'fsu_id' in the attached mysql view

I am receiving Error Code: 1060 Duplicate column name 'fsu_id' in the attached view that I am trying to create even the field names are pre-fixed.
I am creating the view using tool called SQLyog.
The tables bkd and rcs are actually views I have previously created.
CREATE
/*[ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}]
[DEFINER = { user | CURRENT_USER }]
[SQL SECURITY { DEFINER | INVOKER }]*/
VIEW `arab_cargo_fsu`.`rcs_after_bkd`
AS
(SELECT
`bkd`.`fsu_id`
, `bkd`.`msg_id`
, `bkd`.`msg_date_time`
, `rcs`.`fsu_id`
, `rcs`.`msg_id`
, `rcs`.`msg_date_time`
FROM
`arab_cargo_fsu`.`bkd`
LEFT JOIN `arab_cargo_fsu`.`rcs`
ON (`bkd`.`pfx` = `rcs`.`pfx`) AND (`bkd`.`awb` = `rcs`.`awb`));
You can't have a view/table where columns have the same name. Use aliases
SELECT bkd.fsu_id as bkd_fsu_id,
...
rcs.fsu_id as rcs_fsu_id
...

phpmyadmin where condition in mysql union got sintax error

I have tried that in phpmyadmin (4.5.2) and work OK:
(SELECT 'db1' as DB_NAME, db1_col1, null as dbd2_col1 FROM db1.db1_table1)
UNION
(SELECT 'db2' as DB_NAME, null as dbd1_col1, db2_col1 FROM db2.db2_table1)
The result is:
DB_NAME db1_col1 dbd2_col1
db1 DB1_col1_val1 NULL
db2 NULL DB2_col1_val1
But if i try to add a where clause I got sintax error:
(SELECT 'db1' as DB_NAME, db1_col1, null as dbd2_col1
FROM db1.db1_table1
WHERE db1_col1 = 'DB1_col1_val1'
)
UNION
(SELECT 'db2' as DB_NAME, null as dbd1_col1, db2_col1
FROM db2.db2_table1
WHERE db2_col1 = 'DB1_col1_val1'
)
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 'UNION
(' at line 4
This made me crazy!
Nevertheless, when I tried it on console, it worked as espected:
mysql> (SELECT 'db1' as DB_NAME, db1_col1, null as dbd2_col1
-> FROM db1.db1_table1
-> WHERE db1_col1 = 'DB1_col1_val1'
-> )
-> UNION
-> (SELECT 'db2' as DB_NAME, null as dbd1_col1, db2_col1
-> FROM db2.db2_table1
-> WHERE db2_col1 = 'DB1_col1_val1'
-> );
+---------+---------------+-----------+
| DB_NAME | db1_col1 | dbd2_col1 |
+---------+---------------+-----------+
| db1 | DB1_col1_val1 | NULL |
+---------+---------------+-----------+
1 row in set (0.00 sec)
And the question:
Is this a bug of phpmyadmin or am I missing something else?
Since it worked in the console, it is probably a phpmyadmin bug. My guess is that phpmyadmin tried to insert its limit clause after the closing parentheses of the 1st query.
UPDATE
Apparently, this is the same bug as fixed here. If you upgrade to phpmyadmin v4.5.3.0 or later, then this bug is no longer there.
Your use of phpmyadmin documentation indicates you forgot the ; at the end of your request. It was present on the console documentation. Just a thought.

ERROR 1064 (42000) - MySQL error in INSERT ... SELECT query

As part of a MySQL trigger I'm writing, I've got an INSERT ... SELECT query that is returning :
ERROR 1064 (42000) at line 7: 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 'pp2 (curr_code, pricing_id, pid, title, startdate, enddate, priority, enabled) S' at line 33
INSERT INTO product_pricing pp2 (curr_code, pricing_id, pid, title, startdate, enddate, priority, enabled)
SELECT cc, `pp1`.`pricing_id`, `pp1`.`pid`, `pp1`.`title`, `pp1`.`startdate`, `pp1`.`enddate`, `pp1`.`priority`, `pp1`.`enabled`
FROM product_pricing pp1
WHERE pp1.pp_id = NEW.pp_id
ON DUPLICATE KEY UPDATE pp2.pp_id=(SELECT newppid := pp2.pp_id);
I'm not sure if it's the cc part? That's a declared variable in the trigger but it should work given that you should be able to do a SELECT 'hello', t.col1 FROM table t
Any suggestions as to what the error is greatly received.
The INSERT syntax doesn't allow for aliases.
INSERT INTO table [ ( column [, ...] ) ]
{ DEFAULT VALUES | VALUES ( { expression | DEFAULT } [, ...] ) [, ...] | query }
[ RETURNING * | output_expression [ [ AS ] output_name ] [, ...] ]
Remove that pp2 from the INSERT query

create mysql views with UNION operator on query

I am trying to create sql view with a UNION operator on it. I tried executing the sql first before embedding it to the view and the result is success. But when I try it to embed on creating a view it returned this error
"Error Code : 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 'UNION
SELECT p.product_media_id AS media_id, p.product_title AS title, p.product' at line 8
"
I also assure that all columns have the same data type.
SQL view:
CREATE
/*[ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}]
[DEFINER = { user | CURRENT_USER }]
[SQL SECURITY { DEFINER | INVOKER }]*/
VIEW `mydbname`.`s_views`
AS
(SELECT c.media_id AS media_id,
c.title AS title,
c.title_slug AS slug,
c.content_one AS description,
c.type AS cat
FROM content_content c
WHERE c.type = 'news'
OR c.type='travel_genius'
AND c.media_id IS NOT NULL
AND c.status = 1
UNION
SELECT p.product_media_id AS media_id,
p.product_title AS title,
p.product_title_slug AS slug,
p.product_description AS description,
'product' AS cat
FROM p_views p
WHERE p.product_media_id IS NOT NULL);
It is a bug in MySQL. It is the parenthesis that is triggering it:-
http://bugs.mysql.com/bug.php?id=21614