drol55885602 2012-04-27 04:53
浏览 60
已采纳

获取X个特定行。 Mysql查询

Say I have a database with two tables: "food", and "whatToEat".

I query the "whatToEat" and find 3 rows:

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"?

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. This table just holds his name, and the id of the match (or matches since he can be in several at the same time). 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.

Example:

The player Tim query the "mathces_updated" table and find he have 2 new matches that needs to be updated:

   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
  • 写回答

3条回答 默认 最新

  • doujiabing1228 2012-04-27 04:57
    关注
    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?

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么