MYSQL insert from select and variables - mysql

I am trying to insert values coming from a select and variable :
INSERT INTO routeur (`codeAdherent`, `quantiteArticle`, `dateFin`) VALUES
(SELECT `codeAdherent` FROM adherents WHERE categorie = 'G', `quantiteArticle` = $a, `dateFin`= $b);
Write it with and without VALUES, with and without IN, with and without brackets but I always get an synthax error.
Where is my mistake?

Try below:
INSERT INTO routeur (codeAdherent, quantiteArticle, dateFin)
SELECT codeAdherent, #a, #b FROM adherents WHERE categorie = 'G'

You have to read carefully the INSERT syntax because you have got many errors.
This is the right syntax:
INSERT INTO routeur (codeAdherent, quantiteArticle, dateFin)
SELECT codeAdherent, '$a', '$b'
FROM adherents
WHERE categorie = 'G'
PS: To avoid the SQL Injection you should use Prepared Statements

You can try this out :
INSERT INTO routeur (codeAdherent, quantiteArticle, dateFin) VALUES
(SELECT codeAdherent FROM adherents WHERE categorie = 'G', $a, $b);

Related

Inserting a variable into MySQL with Go

I have these 2 variables here
name := request.FormValue("username")
pass := request.FormValue("password")
I want to insert those 2 variables into my database
db.Query("INSERT INTO `godb` (`Username`, `Password`) VALUES ( )")
I tried (name,pass) ('name','pass') ($name, $pass) , none of them work.
Hope the question is not stupid, but I've been looking for solutions online but I did't understand them. Thanks !
From Using Prepared Statements
Parameter Placeholder Syntax
The syntax for placeholder parameters in prepared statements is
database-specific. For example, comparing MySQL, PostgreSQL, and
Oracle:
MySQL PostgreSQL Oracle
===== ========== ======
WHERE col = ? WHERE col = $1 WHERE col = :col
VALUES(?, ?, ?) VALUES($1, $2, $3) VALUES(:val1, :val2, :val3)
You tried PostgreSQL syntax but you use MySQL.
query should be in this format
db.Query("INSERT INTO table ($1, $2) VALUES (column1, column2)", value1, value2)
in your case something like that
db.Query("INSERT INTO godb ($1, $2) VALUES (username, password)", name, pass)

Use of `if( ... = ... )` in mysql?

insert into foo_table (fname, lname, number)
values ('John', 'Doe', if(123 = 456));
For the above MySQL query, can somebody kindly explain what the if(123 = 456) is doing? I currently struggle to see an if statement without a body (i.e. if(condition){ // do something });
The query is syntactically not correct as per mysql version 8, The syntactically correct query is insert into foo_table (fname, lname, number) values ('John', 'Doe', if(123 = 456,1,2)). This will insert 1 if the condition (123 =456) is true, otherwise it will insert 2.

Using value in a variable?

I have the following code:
insert conversion (h1, g1, s1)
values(
'14',
(select g1 from conversion where h1 = '14'),
'15');
I have to create tons of inserts manually and only want use variable for the values - less typos that way.
So want to do something like:
val1 = '14'
val2 = '15'
insert conversion (h1, g1, s1)
values(
val1,
(select g1 from conversion where h1 = val1),
val2);
How can I do this?
You can do somethink like that:
insert conversion (h1, g1, s1)
(select '14',g1,'15' from conversion where h1 = 14)
From the posting above - this did the trick:
http://dev.mysql.com/doc/refman/5.0/en/user-variables.html

How to use $RANDOM (linux) in a mySQL command-line query?

I am trying to insert random values into a table from my linux terminal, but when i use the following SQL statement,
INSERT INTO kCreate (k1 , k2) VALUES ('$RANDOM' , '$RANDOM');
where k1 and k2 are of datatype INT, 0 is being inserted instead of a random value, What am i doing wrong here ?
k1 and k2 are of INT type, no need to put the value inside single quote, try this:
sql="INSERT into kcreate ( k1, k2) values ($RANDOM, $RANDOM);"
echo $sql | mysql -ppassword test
You can use rand() function of mysql with ceil(). Here is an example.
INSERT INTO kCreate (k1 , k2)
VALUES (ceil(rand()*1000) , ceil(rand()*1000));

MySQL SELECT only if doesn't match

I'm trying to do a select from a table based on the value of a name field.
I want to only match certain criteria if a record is not based on the first, ie.
if the following matches:
WHERE name='version'
if so return that single row, if not, look for these too:
WHERE name='v' OR name='e' OR name='r'
etc...
Is this possible in a single query?
Many thanks!
SELECT ...
FROM ..
WHERE name = 'version'
UNION ALL
SELECT ...
FROM ...
WHERE NOT EXISTS (
SELECT ...
FROM ..
WHERE name = 'version'
) AND name IN ('v', 'e', 'r', ...)
EDIT:
Not sure how to test if mysql would cache the results of the first query, but assuming you run the query in a new session:
SELECT #cached:=1, ...
FROM ..
WHERE name = 'version'
UNION ALL
SELECT 2, ...
FROM ...
WHERE #cached IS NULL AND name IN ('v', 'e', 'r', ...)
should work
If in the same session you want to clear the #cached with
SELECT #cached:=1, ...
FROM .., (SELECT #cached:=0) x
WHERE name = 'version'
UNION ALL
SELECT 2, ...
FROM ...
WHERE #cached = 0 AND name IN ('v', 'e', 'r', ...)
select ...
from ..
where (name = 'version') or (name in ('v', 'e', 'r', ...))
Though, since you're basing the comparison on the same field, there's no difference between the above version and
...
where (name in ('version', 'v', 'e', 'r', ...))