简体   繁体   English

MySQL从表WHERE值> 0中选择行

[英]MySQL Select Row from table WHERE value > 0

Hey im having trouble constructing this mysql query its stumped me for about a week now im hoping someone has the answer and can help me learn! 嘿,即时通讯在构建此mysql查询时遇到了麻烦,令我难堪了大约一个星期,即时通讯希望有人能提供答案并可以帮助我学习!

my table looks like this: 我的桌子看起来像这样: 我的桌子看起来像这样

What I want Im looking to get all of the values out of my table WHERE the username OR email = 'abc123' AND WHERE the values of attack strength defense agility etc... are > 0 我想要什么,我希望从表中获取所有值用户名或email ='abc123'并且攻击强度防御敏捷性等的值> 0

So basically how could I only get rows where the values in my columns are greater than 0 and the values are associated with the correct username and email 因此,基本上,我如何才能只获得列中的值大于0并且值与正确的用户名和电子邮件相关联的行

any help would be much appreciated! 任何帮助将非常感激! im not sure if i explained this the best way but id love to help clarify! 我不确定我是否以最好的方式解释了这个问题,但我很爱帮助澄清!

here's what i was working with so far 到目前为止,这是我一直在工作的

SELECT Attack, Strength, Defense, Magic, Ranged, Prayer, Runecrafting, Dungeoneering, Construction, Constitution, Agility, Herblore, Thieving, Crafting, Slayer, Hunter, Mining, Smithing, Fishing, Cooking, Firemaking, Woodcutting, Farming, Summoning FROM experience WHERE email =  abc123 OR username = abc123
SELECT Attack, Strength, Defense, Magic, Ranged, Prayer, Runecrafting, Dungeoneering, Construction, Constitution, Agility, Herblore, Thieving, Crafting, Slayer, Hunter, Mining, Smithing, Fishing, Cooking, Firemaking, Woodcutting, Farming, Summoning FROM experience WHERE (email =  "abc123" OR username = "abc123") AND Attack > 0 AND Strength > 0 AND Defense > 0 AND *etc*

And why doesn't this work? 为何不起作用? where (email = 'abc123' OR username = 'abc123') and attack > 0 and strength > 0 and . where (email = 'abc123' OR username = 'abc123') and attack > 0 and strength > 0 and . .

It works like the following if: if( (email == 'abc123' || username == 'abc123') && stength > 0) if( (email == 'abc123' || username == 'abc123') && stength > 0)如果),则它的工作方式如下: if( (email == 'abc123' || username == 'abc123') && stength > 0)

The first parts between "()" with "||" “()”与“ ||”之间的第一部分 means that at last one the conditions inside of it must be true. 表示最后一个条件必须为真。 And && outside means that also the last condition must be true. && outside表示最后一个条件也必须为真。

I also recommend you to use user ID because it's really better than user name/email 我还建议您使用用户ID,因为它确实比用户名/电子邮件好

Your solution 您的解决方案

SELECT Attack, Strength, Defense, Magic, Ranged, Prayer, Runecrafting, Dungeoneering, Construction, Constitution, Agility, Herblore, Thieving, Crafting, Slayer, Hunter, Mining, Smithing, Fishing, Cooking, Firemaking, Woodcutting, Farming, Summoning FROM experience WHERE (email = abc123 OR username = abc123) AND (Attack + Strength + Defense + Magic + Ranged + Prayer + Runecrafting + Dungeoneering + Construction + Constitution + Agility + Herblore + Thieving + Crafting + Slayer + Hunter + Mining + Smithing + Fishing + Cooking + Firemaking + Woodcutting + Farming + Summoning > 0);

Unfortunately, unless you have a column to sum up all the statistics, the only way to achieve you want is to manually compare all, in which case I use + to sum it up. 不幸的是,除非您有一列来汇总所有统计信息,否则要实现的唯一方法是手动比较所有统计信息,在这种情况下,我将使用+对其进行汇总。

Couldn't find a way to get the data out of the database to way I wanted it so I decided it would be better to simply pull out all the data from the table and filter it using php 无法找到一种方法将数据从数据库中移出我想要的方式,所以我决定最好从表中提取所有数据并使用php对其进行过滤会更好

$stmt2 = $DB_con->prepare("SELECT * FROM experience WHERE username = :uname OR email = :email");
$stmt2->execute(array(':uname' => $_SESSION['username'], ':email' => $_SESSION['email']));
$rowExperience = $stmt2->fetch(PDO::FETCH_ASSOC);

$skills = array("Attack", "Strength", "Defense", "Magic", "Ranged", "Prayer", "Runecrafting", "Dungeoneering", "Construction",
                 "Constitution", "Agility", "Herblore", "Thieving", "Crafting", "Fletching", "Slayer", "Hunter", "Mining", "Smithing","Fishing", "Cooking", "Firemaking", "Woodcutting", "Farming", "Summoning");


           for($i = 0; $i < count($skills); $i++) {
                if(intval($rowExperience[$skills[$i]]) > 0) {
                    echo '<td style="color:grey;">' . $skills[$i] . ' - ' . $rowExperience[$skills[$i]] . '</td>';   
                }           

 }

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

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