0

I'm trying to parameterize the following insert with a nested select.

INSERT IGNORE INTO table1 (creation_timestamp, str1, str2) 
    (SELECT now(), "param1", str2 FROM table2 WHERE key = "param2");

I'd like something like

INSERT IGNORE INTO table1 (creation_timestamp, str1, str2) 
    (SELECT now(), ?, str2 FROM table2 WHERE key = ?) 
    VALUES ("param1", "param2");

Anyone know how I can accomplish something like this?

2 Answers 2

2

Not exactly the same, but very similar.

You can use prepared statements: http://dev.mysql.com/doc/refman/5.7/en/sql-syntax-prepared-statements.html

Example:

PREPARE soExample 
FROM 'INSERT 
      INTO usr (id, username, profile_pic) 
      VALUES (NULL, ?, (SELECT name 
                        FROM customers 
                        WHERE id = ? 
                        LIMIT 1))';

SET @uname = "someUserNameForTheExample";
SET @id = "1";
EXECUTE soExample USING @uname, @id;

Or you can user procedure or/and functions as well

FUNCTION

DROP FUNCTION IF EXISTS insertExample$$
CREATE FUNCTION insertExample(userNameVar VARCHAR(255), uID INT(11)) RETURNS BOOLEAN
BEGIN
    INSERT 
    INTO usr (id, username, profile_pic) 
    VALUES (NULL, userNameVar, (SELECT name 
                                FROM customers 
                                WHERE id = uID 
                                LIMIT 1));
    IF ROW_COUNT() > 0 THEN 
        RETURN TRUE;
    ELSE
        RETURN FALSE;
    END IF;                
END$$

FUNCTION USE

SELECT insertExample("SomeUsername" 2);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, like I mentioned in @Tezla 's answer, I'm confined to keeping the queries inside the code (java), so functions/procs are unavailable to me. I also have the inconvenience of having to go through Vert.x for my SQL calls, so I'm not sure if I can use prepared statements as such, but I'm looking into it. Thanks.
As I feared, it looks like the Vert.x 2 mysql mod is extremely limited. github.com/timyates/mod-jdbc-persistor - Just basic queries. I don't see a way to use prepared statements, even when the mod is using them under the hood. I'm afraid I'm stuck with tiny chained queries for the moment, until I can convince the powers that be to let me tear out this Vert.x crap. It's fine for front end REST calls, but it has been nothing but a headache for back end work.
0

Perhaps you should start by reading https://dev.mysql.com/doc/refman/5.7/en/create-procedure.html. As Mysql Functions enables parameterized input for pre-build queries.

2 Comments

I'd love to used stored functions/procs, but my hands are tied for the time being. I have to keep the query logic out of the DB.
look at @VelkoGeorgiev answer, it's well explained.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.