Mysql query insert into only if select count > n

Mysql query insert into only if select count > n



I have 2 queries:


SELECT COUNT(*) FROM a WHERE id = 1
//if row == 1
INSERT INTO a VALUES(fielda) VALUES('value')



Is there a way to merge these two queries into one? I tried with 'IF (count> 0, ..)' and similar, but the query is incorrect.
This involves inserting a new record into the table, taking care not to exceed a pre-set number of records for each field.
In theory it should be similar to an INSERT IF ...



Edit:



@Barmar I tried but I think I did not understand what you wrote (in fact I made a mistake in the query), I try to answer like this:


THE QUERY AFTER YOUR RESPONSE:
INSERT INTO table1 SELECT MAX(id) FROM table1 WHERE field1 = (SELECT id from a WHERE f = field2) HAVING COUNT(*) = 1 (all fields request) VALUES (all values request)

//field1 = id from table2
//field2 = id from another table: associative value

//ORIGINAL QUERY
//FIRST COUNT:
SELECT COUNT(*) from table1 WHERE field1 = (SELECT id FROM table2 WHERE f = field2)
//AFTER THE INSERT:
INSERT INTO table1 (all fields request) VALUES (all values request)



I came to mind this example I try to show you:



TABLE PLAYER: fields(ID, TEAMID, NAME) => (id=int, teamid=int associate to table team, name=varchar)



TABLE TEAM: fields(ID NAME) => (id=int, name=varchar)



Suppose that the players in a team are maximum 20, so you expect maximum 20 records associated by the player table for the same teamid value, or at least this is what we humans think, because for the computer can also be infinite. I was looking for a way to allow the insertion only in the case in which it is actually permissible to insert records, in this case the condition is that in the players table there are less than 21 records per team.





What is fielda? Is it a column from the row in table a where id = 1?
– Barmar
Sep 3 at 23:01


fielda


a


id = 1





I think you need to clarify what you're actually trying to do. Show an example of the original contents of the tables, and what you want to insert.
– Barmar
Sep 3 at 23:29





An INSERT statement either uses VALUES to list the values to be inserted, or SELECT to get the values from a query. You can't use both in the same insertion.
– Barmar
Sep 3 at 23:30


INSERT


VALUES


SELECT





fielda (in the first example) is a common field of first table. you have right I was wrong to write: don't exist b, exist only a
– Question
Sep 3 at 23:30





Is there one table or two tables? The edit at the bottom has tables named table1, table2 and a.
– Barmar
Sep 3 at 23:33


table1


table2


a




1 Answer
1



You can use INSERT ... SELECT, and put the condition in the SELECT query.


INSERT ... SELECT


SELECT


INSERT INTO player (teamid, name)
SELECT @teamid, @playername
FROM DUAL
WHERE (SELECT COUNT(*) FROM player
WHERE teamid = @teamid) < 20



DUAL is a dummy table when you need to select literal data, but need to include other clauses in the query.


DUAL





Try to see my edit
– Question
Sep 3 at 23:27





Try to see the example
– Question
Sep 3 at 23:55



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ

Node.js puppeteer - Use values from array in a loop to cycle through pages