HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

INSERT/UPDATE in same query from previous SELECT with some singularities

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
samepreviousinsertupdatewithquerysingularitiessomeselectfrom

Problem

I'm trying to perform a INSERT|UPDATE by selecting data from another DB and this is what I have so far:

INSERT INTO pdone.reps 
    (veeva_rep_id,display_name,username,`first`,`last`,email) 
SELECT Id, CONCAT(UCASE(LEFT(firstname, 1)),UCASE(LEFT(lastname, 1)),username, firstname, lastname, email 
FROM veeva.`user` 
WHERE Id = "00580000003UB5VAAW"


  • First problem, I got this error:




[Err] 1064 - You have an error in your SQL syntax; check the manual
that corresponds to your MariaDB server version for the right syntax
to use near 'from veeva.user WHERE Id = "00580000003UB5VAAW"' at
line 1

and I'm not sure what is wrong on the query, any advice?

  • firstname could be JOHN or John or jOhn or any and I want to normalize as John, is UCASE(LEFT(firstname, 1) fine for this? The same apply to lastname



  • If firstname is JOHN and lastname is DOE then username should be John Doe with a space between them, is my CONCATENATE right?



  • I should insert some statics fields like one URL or just VEEVA how I can do that? Values are not present on the query shown here but is just add two more columns to the insert avatar_url and rep_type



  • I am planning to add ON DUPLICATE KEY UPDATE but can be possible to add a restriction based on a column? Lets said UPDATE only if now > lastSyncDate?

Solution

Insert query:

INSERT INTO pdone.reps (veeva_rep_id,display_name,username,first,last,email,avatar_url,rep_type) 
SELECT 
    Id, 
    CONCAT(UCASE(MID(firstname,1,1)),LCASE(MID(firstname,2)),' ',UCASE(MID(lastname,1,1)),LCASE(MID(lastname,2))),
    username, 
    firstname, 
    lastname, 
    email,
    'www.some_static_url.com',
    '1' 
FROM veeva.user 
WHERE Id = "00580000003UB5VAAW"


It will print Only first letter capital from both First & Last name.

Code Snippets

INSERT INTO pdone.reps (veeva_rep_id,display_name,username,first,last,email,avatar_url,rep_type) 
SELECT 
    Id, 
    CONCAT(UCASE(MID(firstname,1,1)),LCASE(MID(firstname,2)),' ',UCASE(MID(lastname,1,1)),LCASE(MID(lastname,2))),
    username, 
    firstname, 
    lastname, 
    email,
    'www.some_static_url.com',
    '1' 
FROM veeva.user 
WHERE Id = "00580000003UB5VAAW"

Context

StackExchange Database Administrators Q#105386, answer score: 4

Revisions (0)

No revisions yet.