简体   繁体   English

获取X特定行数。 MySQL查询

[英]Get X number of specific rows. Mysql query

Say I have a database with two tables: "food", and "whatToEat". 假设我有一个包含两个表的数据库:“食物”和“ whatToEat”。

I query the "whatToEat" and find 3 rows: 我查询“ whatToEat”并找到3行:

id    Food     username
 1    Apple      John
 2    Banana     John
 3    Milk       Linda

If I want to get those from the "food" table, I can just do something like this i guess: 如果我想从“食物”表中获取那些,我可以做这样的事情,我猜:

SELECT * 
  FROM food 
 WHERE username='John' AND typeOfFood = 'apple' 
    OR typeOfFood = 'Banana' OR typeOfFood = 'Milk'

... but is it possible to dynamically write this, since the "whatToEat" table will change all the time, or do I need a loop and query the "food" table one by one for each of the objects in "whatToEat"? ...但由于“ whatToEat”表将一直更改,因此可以动态编写此代码,还是我需要循环并针对“ whatToEat”中的每个对象逐一查询“ food”表?

EDIT 编辑

The above is just an example, the real scenario is an online game. 以上仅是示例,实际场景是在线游戏。 When it's a players turn in a game, he's put on the "matches_updated" table. 当玩家进入游戏时,他被放在“ matches_updated”表中。 This table just holds his name, and the id of the match (or matches since he can be in several at the same time). 该表仅保留他的名字和比赛的ID(或多个比赛,因为他可以同时出现在多个比赛中)。 When a player recive an update, I would like to check if he have any matches that needs to be updated (query "matches_updated" table), and then pull the data and return to him from the "matches" table, where all the information is stored about the matches. 当玩家获得更新时,我想检查他是否有任何需要更新的比赛(查询“ matches_updated”表),然后提取数据并从“ matches”表中返回所有信息存储有关比赛的信息。

Example: 例:

The player Tim query the "mathces_updated" table and find he have 2 new matches that needs to be updated: 玩家Tim查询“ mathces_updated”表,发现他有2个需要更新的新比赛:

   match_id   username
    1          Tim
    2          Tim
    2          Lisa
    1           John
    3           John

... He now want to get the information about these matches, which is stored in the "matches" table: ...他现在想获取有关这些比赛的信息,该信息存储在“比赛”表中:

match_id   match_status player1Name Player1Score Player2Name Player2Score
   1           1           John         123         Tim          12
   2           1           Lisa          4          Tim          15
   3           1           John          0          Lisa         0

I am not sure whether I understand the question correctly. 我不确定我是否正确理解了这个问题。

Actually It depends on the queried tables have what in common. 其实这取决于查询表有什么共同点。

so if suppose food is a common column, then query something like this... 因此,如果假设食物是一个普通的列,则查询类似这样的内容...

select * from food where food in (select food from whattoeat where username = ?)

Try it out, if it solves your problem... 如果能解决您的问题,请尝试一下...

SELECT * FROM matches_updated JOIN matches
         ON matches_updated.match_id == matches.match_id
         WHERE matches_updated.user == "Tim"

-- -

Perhaps you want a JOIN statement ? 也许您想要一个JOIN语句

SELECT * FROM food JOIN whattoeat
         ON food.username == whattoeat.username
         WHERE food.username == "John"

I'm having a really hard time trying to understand what your desired result is - posting example of both tables in question, and the desired result of your query, might help. 我很难理解您想要的结果是什么-发布有关两个表的示例以及您查询的期望结果可能会有所帮助。

SELECT * FROM food WHERE username='John'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM