MySQL的 - 结合INSERT,价值观和选择?价值观、MySQL、INSERT

2023-09-04 03:43:18 作者:宁负天下不负你

我有点新的SQL,而我真的只与它交互足以让一个高分数据库工作。我使用PHP脚本来访问我的网上的MySQL数据库。反正。

I'm kind of new to SQL, and I'm really only interacting with it enough to get a high score database working. I'm using a php script to access my online MySQL db. Anyways.

我所拥有的是2桌,球员和得分。玩家有一个id,uniqueDeviceId和chosenname。 分数有通常的东西你几乎期望。

What I have is 2 tables, Player and Score. Player has an id, uniqueDeviceId, and chosenname. Score has the usual things youd expect.

我真的很想做一个查询(以降低复杂性...)是结合的语法

What I'd really like to do in a single query (to reduce complexity...) is to combine the syntaxes of

INSERT INTO scores VALUES
and INSERT INTO scores SELECT...

成某种怪物,像

Into some sort of monster, like

INSERT INTO scores(score,difficulty,playerid)
   VALUES(TheScoreThatImProviding,TheDifficultyThatImProviding, (SELECT 
       id FROM player WHERE uniqueDeviceId = TheUniqueIdThatImProviding)
     )

如果这是很有意义的。我想查找的playerId(第三届值),并与提供的值的输入组合,选择的结果。

If that makes sense. I want to lookup the playerId (3rd "value"), and mix the result of that select with the input of the VALUES provided.

这可能吗?所有的谷歌搜索结果香港专业教育学院发现无论是有它的所有值或全部选择。

Is this possible? All the googling results ive found either have it all VALUES or all SELECT.

推荐答案

有道理,被称为的插入SELECT 。这对uniqueDeviceId 123为例查询,比分5和难度易:

Makes sense and is called INSERT SELECT. This is an example query for the uniqueDeviceId 123, Score 5 and Difficulty 'easy':

INSERT INTO scores (score, difficulty, playerid)
  SELECT 5, 'easy', id
  FROM player WHERE uniqueDeviceId = 123;