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

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`

Related

ERROR 1064 (42000) - can't find the mistake in the SQL query

I've table named networks:
mysql> describe networks;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| range | varchar(45) | NO | | NULL | |
+-------+-------------+------+-----+---------+----------------+
I'm trying to INSERT value in this table with the following query:
INSERT INTO networks(range) VALUES("10.10.10.10/24");
However I get this error:
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 'range) VALUES("10.10.10.10/24")' at line 1
I tried playing with the quotes (changing them form " to ', and similar stuff), however it didn't work. Any ideas what may be wrong?
You can use the following INSERT:
INSERT INTO `networks` (`range`) VALUES ('10.10.10.10/24');
You can enclose table and column names into backticks to avoid conflicts with reserved words on MySQL. In your case RANGE is a reserved word and can't be used as a column or table name without backticks. A string value should be enclosed in single-quotes.
Note: You should avoid using reserved words like RANGE as column and table definition. You can find a list of all reserved words on the official docs.
Try Back Ticks:
INSERT INTO networks(`range`) VALUES('10.10.10.10/23');
This may help.

Bigsql like predicate error

I am writing sql query for bigsql.
If it looks like this
select t.city from table t where t.city like 'A%'
It works ok, but next one fails:
select t.city from table t where t.city like 'A%' escape '\'
I only add escape expression and it give me following error
Error Code: -5199, SQL State: 57067] DB2 SQL Error: SQLCODE=-5199, SQLSTATE=57067, SQLERRMC=Java DFSIO;1;2, DRIVER=4.15.82
I found this documentation http://www-01.ibm.com/support/knowledgecenter/SSPT3X_2.1.2/com.ibm.swg.im.infosphere.biginsights.bigsql.doc/doc/bsql_like_predicate.html?lang=en
So seems escape should work.
If I escape escape character I get another error
Error Code: -130, SQL State: 22019] DB2 SQL Error: SQLCODE=-130, SQLSTATE=22019, SQLERRMC=null, DRIVER=4.15.82. 2) [Error Code: -727, SQL State: 56098] DB2 SQL Error: SQLCODE=-727, SQLSTATE=56098, SQLERRMC=2;-130;22019;, DRIVER=4.15.82
But if I use not '\' character as escape, but something another, like '/' it works fine.
Any ideas why it may happen?
Try this maybe. You might have to escape the escape character.
select t.city from table t where t.city like 'A%' escape '\\'
Based upon this sample:
\connect bigsql
drop table if exists stack.issue1;
create hadoop table if not exists stack.issue1 (
f1 integer,
f2 integer,
f3 varchar(200),
f4 integer
)
stored as parquetfile;
insert into stack.issue1 (f1,f2,f3,f4) values (0,0,'Detroit',0);
insert into stack.issue1 (f1,f2,f3,f4) values (1,1,'Mt. Pleasant',1);
insert into stack.issue1 (f1,f2,f3,f4) values (2,2,'Marysville',2);
insert into stack.issue1 (f1,f2,f3,f4) values (3,3,'St. Clair',3);
insert into stack.issue1 (f1,f2,f3,f4) values (4,4,'Port Huron',4);
select * from stack.issue1;
select * from stack.issue1 where f3 like 'M%';
\quit
I get the following results:
jsqsh --autoconnect --input-file=./t.sql --output-file=t.out
0 rows affected (total: 0.28s)
0 rows affected (total: 0.22s)
1 row affected (total: 0.37s)
1 row affected (total: 0.35s)
1 row affected (total: 0.38s)
1 row affected (total: 0.35s)
1 row affected (total: 0.35s)
5 rows in results(first row: 0.33s; total: 0.33s)
2 rows in results(first row: 0.26s; total: 0.26s)
cat t.out
+----+----+--------------+----+
| F1 | F2 | F3 | F4 |
+----+----+--------------+----+
| 1 | 1 | Mt. Pleasant | 1 |
| 0 | 0 | Detroit | 0 |
| 4 | 4 | Port Huron | 4 |
| 3 | 3 | St. Clair | 3 |
| 2 | 2 | Marysville | 2 |
+----+----+--------------+----+
+----+----+--------------+----+
| F1 | F2 | F3 | F4 |
+----+----+--------------+----+
| 1 | 1 | Mt. Pleasant | 1 |
| 2 | 2 | Marysville | 2 |
+----+----+--------------+----+
This shows your syntax is correct, however, based upon the -5199 error code, this is an issue with the FMP processes not having enough memory or an issue with the Hadoop I/O component. You can get further information on this error by issuing
db2 ? sql5199n
from the command line.
The SQL error message should have directed you to the node where the error occurred and where the Big SQL log file is and the associated reader log files are located.
SQL5199 error generally means an issue with HDFS ( you can do a db2 \? SQL5199 to get details on the message -- as user bigsql ). Check the bigsql and DFS logs to see if that gives any pointers to the problem.
Hope this helps.

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;

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

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