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.
Related
update fare_strategy set sales_date_to = ’07-SEP-22’
where dep_date_to is not null
and market_id in
(select market_id
from fare_strategy_markets
where set_type = ‘STANDARD’
);
In the following query, I'm getting error as SQL command not properly ended.
try to change like this because in your query you used ’ this is not valid so i just change it to ' so it's works fine
update fare_strategy set sales_date_to = '07-SEP-22' where dep_date_to is
not null and market_id in (select market_id from
fare_strategy_markets where set_type = 'STANDARD');
I have a table looked like this
mysql> select * from rc;
+----+----------------------------+----------------------------+----------------------------+
| id | business_date | cutoff_dt | recon_dt |
+----+----------------------------+----------------------------+----------------------------+
| 1 | 2017-03-21 16:50:29.032000 | 2017-03-21 16:50:31.441000 | 2017-03-21 16:50:32.832000 |
+----+----------------------------+----------------------------+----------------------------+
1 row in set (0.00 sec)
I want to run query
-Select * from rc where business_date = '2017-03-17'
- ifcutoff_dt` is null or empty, it will display null, otherwise display not null
I wrote it into shell script.
#! /bin/bash
mysql -u root -p <<rcard
use rcard;
SELECT *
(CASE WHEN (cut_off_dt = "NULL")
THEN
echo "Null"
ELSE
echo "NOT NULL"
END)
from rc WHERE business_date = '2017-03-17';
rcard
But I get error
ERROR 1064 (42000) at line 2: 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 '(CASE WHEN (cut_off_dt = "NULL")
THEN
echo "Null"
ELSE
' at line 2
Is it the correct way to write the IF ELSE in MySQL ?
This is not the correct way to check for NULL values.
Try this:
SELECT *,
IF(mepslog_cut_off_dt IS NULL, 'NULL', 'NOT NULL')
from rc_mepslog
WHERE mepslog_business_date = '2017-03-17';
You can alternatively use a CASE expression:
SELECT *,
CASE
WHEN mepslog_cut_off_dt IS NULL THEN 'NULL'
ELSE 'NOT NULL'
END
from rc_mepslog
WHERE mepslog_business_date = '2017-03-17';
SELECT *, (CASE WHEN (mepslog_cut_off_dt IS NULL)
THEN
'Null'
ELSE
'NOT NULL' END) from rc WHERE business_date = '2017-03-17'
To compare if the value is null, use column IS NULL instead of column = 'NULL'; the latter would check if the column you are comparing has the string 'null'
There's a missing comma after select * and none of your table names you specified in the query exists. To separate a table from the column, you use a dot .. There are other wrong written column names appart from that. Also, you don't use echo, you simply specify what to do. Also, I'd suggest to use an alias for the selected column from the case (AS 'nullOrNotNull')
SELECT * ,
CASE WHEN cut_off_dt IS NULL
THEN
'Null'
ELSE
'NOT NULL'
END AS 'nullOrNotNull'
from rc WHERE business_date = '2017-03-17';
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).
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
mysql> SELECT Ext, Pass, Name, Context FROM temp_Users WHERE temp_Users.Pass NOT IN (SELECT Pass FROM Users);
+------+-------+---------+------------+
| Ext | Pass | Name | Context |
+------+-------+---------+------------+
| 6003 | Hello | WebPone | DLPN_Admin |
+------+-------+---------+------------+
1 row in set (0.00 sec)
mysql> UPDATE Users
-> SET (Pass, Name, Context) = (SELECT Pass, Name, Context FROM temp_Users WHERE temp_Users.Pass NOT IN (SELECT Pass FROM Users))
-> WHERE Users.Ext = temp.Ext;
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 '(Pass, Name, Context) = (SELECT Pass, Name, Context FROM temp_Users WHERE temp_' at line 2
I want to update my database from Select result and i am getting this error. Please tell me how i can resolve it ?
MySQL doesn't support the SET ( multiple_fields ) = ( subquery_that_returns_multiple_fields ) syntax for UPDATE statements. Instead, you have to use a "multiple-table" update (a join). See http://dev.mysql.com/doc/refman/5.6/en/update.html.
Your query has some other problems as well, so I'm not clear on exactly what you want . . . but I think you want something like this:
UPDATE users
JOIN temp_users
ON temp_users.ext = users.ext
SET users.pass = temp_users.pass,
users.name = temp_users.name,
users.context = temp_users.context
WHERE temp_users.pass NOT IN
-- extra subquery to bypass MySQL limitation:
( SELECT pass
FROM ( SELECT pass
FROM users
) t
)
;
UPDATE Users u
JOIN temp_Users tu ON tu.Ext = u.Ext
SET
Pass = tu.Pass,
Name = tu.Name,
Context = tu.Context
WHERE tu.Pass NOT IN (
SELECT Pass
FROM Users
)