Update my database from Select result - mysql

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
)

Related

SQL run command if row exists

I'm new to MySQL and I'm trying to make the following pseudocode work:
SELECT IF(
EXISTS(SELECT * FROM users WHERE `email`="admin" AND `token`="blablabla"),
(UPDATE * FROM sometable WHERE `var`="notimportant"),
"NOT_AUTHORIZED");
What I'm trying to achieve is running code based on the presence of a row, and if it doesn't exists return a message, or something usable. If it does exists, run another SQL command instead, and return those results.
Is this possible?
Your intent is a bit hard to follow from the invalid syntax. But the gist of your question is that you can use a where clause:
UPDATE sometable
SET . . .
WHERE var = 'notimportant' AND
EXISTS (SELECT 1 FROM users WHERE email = 'admin' AND token = 'blablabla');
You can also represent this as a JOIN. Assuming the subquery returns at most one row:
UPDATE sometable t CROSS JOIN
(SELECT 1
FROM users
WHERE email = 'admin' AND token = 'blablabla'
LIMIT 1
) x
SET . . .
WHERE var = 'notimportant' ;

How to list tables used in a view with mysql

I need a list of tables being used in a view in mysql.
For example, if I have a view like:
SELECT * FROM table1
JOIN table2
ON table1.id = table2.id
I want to get: table1,table2
Unfortunately, I don't believe that's possible directly. Instead, you need to query and parse the actual view definition:
SELECT VIEW_DEFINITION
FROM INFORMATION_SCHEMA.VIEWS
WHERE
TABLE_NAME = ?;
.
mysql> CREATE VIEW vw_test AS
-> SELECT * FROM table1 JOIN table2 ON table1.id = table2.id;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT VIEW_DEFINITION FROM INFORMATION_SCHEMA.VIEWS
-> WHERE TABLE_NAME = 'vw_test';
+------------------------------------------------------------------+
| VIEW_DEFINITION |
+------------------------------------------------------------------+
| select * from table1 join table2 on table1.id = table2.id; |
+------------------------------------------------------------------+
1 row in set (0.00 sec)
then you could use any of the following tools to parse the table names:
Terence Parr's ANTLR parser generator (Java, but can generate parsers in any one of a number of target languages) has several SQL grammars available, including a couple for PL/SQL, one for a SQL Server SELECT statement, one for mySQL, and one for ISO SQL - (http://www.antlr.org/grammar/list).
I took this from SO answer here: SQL parser library for Java - Retrieve the list of table names present in a SQL statement
Data Tools Project - SQL Development Tools (http://www.eclipse.org/datatools/project_sqldevtools/).
Here's the documentation for the SQL Query Parser (http://www.eclipse.org/datatools/project_sqldevtools/sqltools_doc/SQL%20Query%20Parser%20User%20documentation.htm).
Here's a blog with descriptions of how to "Get columns and tables in SQL script (Java version)" http://www.dpriver.com/blog/list-of-demos-illustrate-how-to-use-general-sql-parser/get-columns-and-tables-in-sql-script/
Or write yourself a custom mySQL proc based on the following (found here - http://www.sqlparser.com/fetch-table-column-name-example-extact-all-table-field-name.php):
SELECT c_mandant, hist_datum, parkey1, parkey2, funktionscode, ma_parkey, me_parkey
, CASE WHEN EXISTS (SELECT 1
FROM CDS_H_GRUPPE GRP1
WHERE GRP1.c_mandant = c_mandant
AND GRP1.hist_datum = ADD_MONTHS(LAST_DAY(TRUNC(SYSDATE)), -1)
AND GRP1.funktionscode = 'H'
AND GRP1.parkey1 = ma_parkey)
THEN 1
ELSE NULL
END MA_ME
, CASE WHEN EXISTS (SELECT 1
FROM CDS_H_GRUPPE GRP2
WHERE GRP2.c_mandant = c_mandant
AND GRP2.hist_datum = ADD_MONTHS(LAST_DAY(TRUNC(SYSDATE)), -1)
AND GRP2.funktionscode = 'U'
AND GRP2.parkey1 = me_parkey)
THEN 1
ELSE NULL
END ME_MA
, ROW_NUMBER() OVER (PARTITION BY c_mandant, ma_parkey, me_parkey ORDER BY c_mandant, ma_parkey, me_parkey) ANZ_MA
FROM (SELECT c_mandant, hist_datum, parkey1, parkey2, funktionscode
, CASE WHEN funktionscode = 'U'
THEN parkey1
ELSE parkey2
END MA_PARKEY
, CASE WHEN funktionscode = 'U'
THEN NULL
ELSE parkey1
END ME_PARKEY
FROM
CDS_H_GRUPPE
WHERE
funktionscode IN ('U', 'H')
AND hist_datum = ADD_MONTHS(LAST_DAY(TRUNC(SYSDATE)), -1)
)
this is what you want... this can get table used in Views and table which joined together.. but it can get one join... if want more add few more hint..
hope this would solve your question...
select
case
when view_definition regexp '.*from +.*'
then substring_index(substring_index(view_definition, 'from ', -1), ' ', 1)
end as 'primary table',
case
when view_definition regexp '.*join +.*'
then substring_index(substring_index(view_definition, 'join ', -1), ' ', 1)
end as 'joined table'
from information_schema.views where table_name="YOUR VIEW NAME" and table_schema="shotbot_production";
No that is not possible. You have to look for the definition of the view and get that done by yourself manually.

Update a row with data from another row within the same table

UPDATE user SET
tw_oauth_token=(SELECT tw_oauth_token FROM user WHERE id=27),
tw_oauth_token_secret = (SELECT tw_oauth_token_secret FROM user WHERE id=27),
tw_user_id = (SELECT tw_user_id FROM user WHERE id=27),
handler = (SELECT handler FROM user WHERE id=27),
merged=1 WHERE id=26
The idea is to select data from user table where id = 27 and update the same table where id = 26.
I am having the following error:
#1093 - You can't specify target table 'user' for update in FROM clause
Any help would be appreciated. Thanks
UPDATE user a
CROSS JOIN user b
SET a.tw_oauth_token = b.tw_oauth_token,
a.tw_oauth_token_secret = b.tw_oauth_token_secret,
a.tw_user_id = b.tw_user_id,
a.handler = b.handler,
a.mrged = 1
WHERE a.ID = 26 AND
b.ID = 27
Why don't you try to create a virtual table based on your table then update the user table with SELECT statements on that View. Something like this:
CREATE VIEW view_user AS
SELECT *
FROM user;
And then use the view_user in the update.

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

Complex UPDATE query in SQL Server 2008, rewrite from Oracle 10g

I need to re-write the following Oracle 10g query to work in SQL Server 2008
It's an update query, where some field are retrieved from a SELECT and some are given (from code).
UPDATE "BMAN_SQL"."CELLS_GLIST"
SET ("GLIST_ID", "GLIST_VALUE_ID") = (
SELECT "GLIST_ID", "GLIST_VAL_ID"
FROM "BMAN_SQL"."GLISTS_VAL_UOR"
WHERE ("UOR_ID"=3)
AND ("GLIST_CODE"='X')
),
"SESSION_ID" = 1553245736,
"USER_ID" = 13
WHERE EXISTS ( SELECT * FROM ... )
Note that I need to use the UPDATE SET ... WHERE EXIST ... structure for compatibility with Oracle (query are automatically built by a QueryBuilder class for each specific DBMS).
I also cannot write:
UPDATE "BMAN_SQL"."CELLS_GLIST"
SET ("GLIST_ID", "GLIST_VALUE_ID", "SESSION_ID", "USER_ID") = (
SELECT "GLIST_ID", "GLIST_VAL_ID", 1553245736, 13
FROM "BMAN_SQL"."GLISTS_VAL_UOR"
WHERE ("UOR_ID"=3)
AND ("GLIST_CODE"='X')
)
WHERE EXISTS ( SELECT * FROM ... )
because (as per this old thread Oracle "Cannot update to NULL") it returns an error if the SELECT does not fetch any record.
Thanks in advance!
You need to join update query in this case. Please see syntax below. <Your condition here> Replace this section with your join condition here.
UPDATE m
SET
GLIST_ID = r.GLIST_ID
,GLIST_VALUE_ID = r.GLIST_VAL_ID
, SESSION_ID= 1553245736
, USER_ID = 13
from BMAN_SQL.CELLS_GLIST m
inner join BMAN_SQL.GLISTS_VAL_UOR r on <Your condition here>
WHERE
r.UOR_ID=3 AND (r.GLIST_CODE='X') AND
EXISTS ( SELECT * FROM ... )
EDIT
UPDATE BMAN_SQL.CELLS_GLIST
SET
GLIST_ID = (SELECT TOP 1 GLIST_ID FROM BMAN_SQL.GLISTS_VAL_UOR WHERE (UOR_ID=3) AND (GLIST_CODE='X'))
,GLIST_VALUE_ID = (SELECT TOP 1 GLIST_VAL_ID FROM BMAN_SQL.GLISTS_VAL_UOR WHERE (UOR_ID=3) AND (GLIST_CODE='X'))
,SESSION_ID = 1553245736
,USER_ID = 13
WHERE EXISTS ( SELECT * FROM ... )