简体   繁体   English

根据另一个表中的值从一个表中选择值

[英]Select value from one table based on value from another

I don't have my database populated yet, I'm still in the process of designing it. 我还没有填充数据库,我仍然在设计它。 What I am doing is making a database based on a game, and I'm building a program to crawl through some data to fill the database. 我正在做的是建立一个基于游戏的数据库,我正在构建一个程序来抓取一些数据来填充数据库。 So, here's how the database is laid out. 那么,这是数据库的布局方式。

Item Table
id, name, image

Stat Table
id, item_id, stat_name, stat_value

I have one of the stat names set to Class , and the stat value set to Warrior . 我将其中一个统计名称设置为Class ,并将stat值设置为Warrior I want to be able to select all items that have a stat of warrior selected. 我希望能够选择所有具有战士属性的项目。 With my limited knowledge of SQL, this seems like it would require multiple queries, but I know I'm wrong. 由于我对SQL的了解有限,这似乎需要多次查询,但我知道我错了。

I'm not great with joins, and I think that is where I would have to do it, but again, not so great with SQL. 我对加入并不是很好,而且我认为这是我必须要做的事情,但同样,SQL也不是很好。 Any help would be greatly appreciated. 任何帮助将不胜感激。 Also, both the stat_name and stat_value would have to be queries. 此外, stat_name和stat_value都必须是查询。 So stat_name='Class' stat_value='Warrior' . 所以stat_name='Class' stat_value='Warrior'

SELECT DISTINCT i.*
FROM Item i
  JOIN Stat s
    ON i.id = s.id
WHERE s.stat_name = 'Class' 
  AND s.stat_value = 'Warrior'

This should work if I've interpreted your database idea. 如果我已经解释了您的数据库想法,这应该有效。

I want to say that I think that your database design is flawed, your Stat table is going to be bloated and will be a pain to query with. 我想说我认为你的数据库设计存在缺陷,你的Stat表会变得臃肿,查询起来会很麻烦。 Have you considered a table for each class? 你考虑过每个班级的桌子吗?

     select i.*
       from item_table i
 inner join stat_table s
         on i.id = s.item_id
      where s.stat_name = 'Class' 
        and s.stat_value = 'Warrior'

if you don't need the id, just replace i.* with i.name, i.image 如果你不需要id,只需用i.name, i.image替换i.*

Not 100% if I follow, but I'll give it a shot. 如果我遵循,不是100%,但我会试一试。 So you want to load rows from the Stat table and join the Item table? 所以你想从Stat表加载行并加入Item表?

SELECT * FROM Stat
    LEFT JOIN Item 
        ON Item.id = Stat.item_id 
WHERE Stat.stat_name='Class'
AND Stat.stat_value='Warrior'

暂无
暂无

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

相关问题 select 很少从一个表中获取并基于从另一个表中获取的值 - select few from one table and based on value fetch from another table 如何根据另一个表中的公共id值选择一个表中的记录? - How to select records in one table based on a common id value from another table? 根据另一个选择中的值从一个选择中删除选项 - Removing options from one select, based on the value in another select 根据另一个表的最小值选择表的属性 - Select attribute of table based on min value from another table 从一个表中选择并包括来自另一个表的总和 - Select From One Table And Include Sum Value From Another 如何在一个 select 选项中获取选定值,并在此基础上,从 MySQL 表中获取数据,以相同形式显示另一个 select 选项 - How to get selected value in one select option and based on that, fetch data from MySQL table to show another select option in the same form 如何根据从Symfony2中的另一个列表中选择的值从一个选择列表中获取值? - How to get a value from one select list based on value selected from another list in Symfony2? 从一个表中选择所有行,并为另一表中的每一行选择一个特定值 - Select all rows from one table and one specific value for each row from another table 从两个表中各选择一个记录。 通过将另一个值与新表匹配来更新一个值 - select one record each from two table. update the one value by matching the another one to a new table 从一个表中选择行,其中从另一个表中的数组中存在值 - select row from one table where value exists from array from another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM