Why am I getting this syntax-error while trying to update a attribute using mysql-query? - mysql

I am trying to update 'pwd' column of 'Goal' table using following mysql query but sad thing is it gives error. mysql query:
UPDATE Goal
SET pwd = (SELECT MD5(CONCAT(ID, LEFT(phone,3), '$='))
FROM Esubmission);
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 'FROM Esubmission)' at line 1
My tables are like below;
Esubmission
ID | pnone | email |
--------------------------------------------
20378 | 00000000000 | someone#gmail.com|
20379 | 00000000000 | someone#gmail.com|
20380 | 00000000000 | someone#gmail.com|
Goal
usr | pwd | time|
--------------------
20378 | |12:09|
20379 | |15:29|
20380 | |15:47|

Hmm.... I'm thinking that you really want to set the password for each user (I think), in which case you'll need to have a join for the update, and that way you can eliminate your multiple values you are selecting.. because the way you have it, you're updating all the values in the Goal table for pwd to the same value... the way below, you update each users pwd to be based on the row joined with the ID. Assuming you have one row for each usr in the esubmission table, this should work:
UPDATE Goal
SET pwd = MD5(CONCAT(ID, LEFT(e.phone,3), '$='))
FROM Goal g, Esubmission e
where e.ID = g.usr

I tested it here and worked fine. MySQL 5.1.37

Related

How to convert select statement result to JSONARRAY and update another table

I want to convert select result to JSON and write it to another table:
update patrol_patrol a, position_user b
set a.route = json_array(select coordinate from b )
where a.id = 1;
and get error:
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 coordinate from b ) where a.id = 1' at line 2
select route from patrol_patrol;
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| route |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| ["112.58006496213066,22.311484443420195"] |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set
select coordinate from position_user;
+---------------------------------------+
| coordinate |
+---------------------------------------+
| 112.701036,22.738611 |
| 112.701036,22.738632 |
| 112.701036,22.738632 |
| 112.701036,22.738652
position_user.coordinate should be ["112.701036,22.738611", "112.701036,22.738632", "112.701036,22.738652", ....] after update
Since you're only updating the patrol_patrol table, you should only include that in the first part of your update statement. To get what you're looking for, I recommend using the JSON_ARRAYAGG function, which will combine your results into one array, which can then be used to assign the result to a.route:
UPDATE patrol_patrol a
SET a.route = (SELECT JSON_ARRAYAGG(coordinate) FROM position_user)
WHERE a.id = 1;
A dbfiddle can be found here demonstrating this approach.

Ubuntu 16.04,MySQL 5.7.19,Unable to delete database

I am new to MySQL and in the learning curve ,
I wanted somehelp ..
I tried importing the temp database from https://github.com/datacharmer/test_db ... I am having diffulties in importing the same .
I think I have wrongly imported and I wanted to drop the table , But its not allowing me .
I get the below error .
mysql> show databases;
+-------------------------+
| Database |
+-------------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| #mysql50#test_db-master |
+-------------------------+
5 rows in set (0.00 sec)
I am not getting any response in Terminal , but when I tried in MySQL Workbench , I am getting the below error
*drop '#mysql50#test_db-master'
22:04:30 drop '#mysql50#test_db-master' 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 ''#mysql50#test_db-master'' at line 1 0.00034 sec*
I wanted to delete this table & wanted to create a new one again ...
https://dev.mysql.com/doc/refman/5.7/en/drop-database.html shows the syntax for DROP DATABASE.
Pro tip: Read documentation. You can get answers more quickly than by posting on Stack Overflow.

Copying all entries from one table to another with a hard coded value

I currently am copying multiple tables from different mysql schemas into 1 table. While trying to copy all of the entries, I am having issues with the "Insert" into temp table.
cua010.doc_table
| ID | _FilePath |
testing.temp_entries
| ID | File | Schema |
Here is my query
INSERT INTO testing.temp_entries (File, Schema )
SELECT _FilePath, 'CU010'
FROM cua010.doc_table
In the end I would like to results to be
| ID | File | Schema |
| 1 | test | cua010 |
| 2 | test2| cua010 |...
This is the error message i get
0 84 14:49:47 INSERT INTO testing.temp_entries (File, Schema )
SELECT _FilePath, 'cua010'
FROM cua010.doc_table 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 'Schema)
SELECT _FilePath, 'cua010'
FROM cua010.doc_table' at line 1 0.031 sec
use "`" around schema (alt +96 in windows)
INSERT INTO testing.temp_entries (File, `Schema` )
SELECT _FilePath, 'cua010'
FROM cua010.doc_table ;
Schema is a reserved word.
INSERT INTO `testing`.`temp_entries` (`File`, `Schema` )
SELECT `_FilePath`, 'CU010'
FROM `cua010`.`doc_table`

Compound MySQL query issues

First, I would like to tell about the basic structure that I am following. Say I have a table named td_idea which has a structure like this:
|-------------------------------------------------------------------|
| idea_id | idea_name | idea_submitter_id | idea_status |
|----------|--------------|----------------------|------------------|
Then I have a table called td_idea_contribution which is to show the contribution that user s have made on a particular idea:
|-------------------------------------------------|
| contribution_id | idea_id | submitter_id |
|------------------|------------|-----------------|
And finally the td_idea_contribution_like table:
|-------------------------------------------------|
| like_id | contribution_id | submitter_id |
|----------|--------------------|-----------------|
I want to display all the contribution based on a particular idea_id,
each contribution listed will also show the number of votes on that particular contribution.
Here's what I am trying to achieve using the query:
$sql="SELECT td_idea.*,td_idea_contribution.*,COUNT(td_idea_contribution_like.*) AS tot_like
FROM td_idea,td_idea_contribution,td_idea_contribution_like
WHERE td_idea.idea_id=td_idea_contribution.idea_id
AND td_idea_contribution.contribution_id=td_idea_contribution_like.contribution_id
AND td_idea.idea_id='$id'
AND td_idea_contribution.contribution_type='Design' ORDER BY tot_like DESC";
but the SQL string is showing me error. The error is as follows
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 td_idea_contribution_like.tot_like FROM' at line 1
COUNT(td_idea_contribution_like.*) is the problem here
* means You specified more than one column here.

SET a variable in SELECT statement - MySQL

I'm using this code which has an error:
SET #rejects = '';
SELECT *
FROM list
WHERE maker = 1
AND by_ids IN ('10','11')
AND country LIKE '%I%'
AND (
src IS NULL
|| src NOT IN (#rejects)
AND checkSrc(src) = 'yes'
AND SET #rejects = CONCAT(#rejects,',',src)
);
What's causing the issue?
The issue is that you cannot mix select and set in one statement, there'll surely be syntax error:
select*from t where 1 and set#a=1;
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 'set#a=1' at line 1
If you want to do set within select, use the colon equals syntax. Change this:
select*from t where 1 and set#a=1;
into:
select*,#a:=1 from t where 1;
Here's how you update the variable upon each row:
create table t(id int); insert t values(1),(2),(3);
set#a=0;
select#a:=id from t;
+--------+
| #a:=id |
+--------+
| 1 |
| 2 |
| 3 |
+--------+
And you can even do concat:
set#a='0';
select #a:=concat(#a,',',id)from t;
+-----------------------+
| #a:=concat(#a,',',id) |
+-----------------------+
| 0,1 |
| 0,1,2 |
| 0,1,2,3 |
+-----------------------+
Or concat without the leading 0:
set#a='';
select #a:=concat(#a,if(#a='','',','),id)from t;
+------------------------------------+
| #a:=concat(#a,if(#a='','',','),id) |
+------------------------------------+
| 1 |
| 1,2 |
| 1,2,3 |
+------------------------------------+
However, the manual explicitly states that this is dangerous:
...you should never assign a value to a user variable and read the
value within the same statement...
...you might get the results you expect, but this is not
guaranteed.
...the order of evaluation for expressions involving user variables is
undefined.
This has also been mentioned on Xaprb.
Lastly, if you're doing quirky things like assigning differing value types to the variable and etc, checkout the manual to be sure you understand the intricate mechanisms.
Then you might write your query like this.
SET #rejects = '';
SELECT #rejects = CONCAT(#rejects,',',src) FROM list WHERE maker = 1 AND by_ids IN ('10','11') AND country LIKE '%I%' AND
(src IS NULL OR src NOT IN (#rejects) AND checkSrc(src) = 'yes');
SELECT #rejects;