1

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?

3
  • What types are $a and $b? Commented Oct 1, 2018 at 10:39
  • $a is an integer, $b a date Commented Oct 1, 2018 at 10:41
  • What values you want to insert in which fields? Your insert query is definitely wrong. Commented Oct 1, 2018 at 10:44

3 Answers 3

1

Try below:

INSERT INTO routeur (codeAdherent, quantiteArticle, dateFin) 
SELECT codeAdherent, @a, @b FROM adherents WHERE categorie = 'G'
Sign up to request clarification or add additional context in comments.

6 Comments

no change, to the try I write quantiteArticle = 1, dateFin = "2018-12-31"
what error it shows, I've edited the answer you can check now - syntax error was is where cluase
quantiteArticle and dateFin doesn't come from adherents. There are external values.
you can check now
Fantastic, I wrote : INSERT INTO routeur (codeAdherent, quantiteArticle, dateFin) SELECT codeAdherent, 1, "2018-12-31" FROM adherents WHERE categorie = 'G' and that's work. I don't really understand the synthax but the result is here. A question : why do you change $ to @ ?
|
0

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

4 Comments

I want to insert not only codeAdherent but also quantiteArticle and dateFin
@Chamarejc check now
quantiteArticle and dateFin doesn't come from adherents. There are external values. Thanks for your suggestion about prepare statements
@Chamarejc in your original SQL it seems that quantity and datafin were a condition with category. Anyway I adjusted my answer.
0

You can try this out :

INSERT INTO routeur (codeAdherent, quantiteArticle, dateFin) VALUES 
(SELECT codeAdherent FROM adherents WHERE categorie = 'G', $a, $b);

1 Comment

here under the answer : INSERT INTO routeur (codeAdherent, quantiteArticle, dateFin) VALUES (SELECT * FROM adherents WHERE categorie = 'G', quantiteArticle = 1, dateFin= "2018-12-31") MySQL a répondu: Documentation #1064 - Erreur de syntaxe près de 'SELECT * FROM adherents WHERE categorie = 'G', quantiteArticle = 1, dateFin=' à la ligne 1

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.