[英]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.