我正试图从两名已经在公司接受录取的关系学生那里回来。为了测试查询,我在表中填充了一小部分记录,但该查询无法正常运行。应用程序状态属性中可能出现4种情况:“接受接受”,“拒绝接受”,“不提供”,“待处理”。
我正在尝试计算“已接受提供”和“已拒绝提供”的数量,并输出接受了2个以上工作机会的学生的姓名。为什么这不起作用?逻辑上看起来应该吗?
我收到的录取学生人数计数错误。我本应该只收到1个学生的时候收到4个学生的名字。如果我添加更多的测试用例,则测试用例将继续以相同的方式运行。
应用程序关系测试数据:
INSERT INTO applications
VALUES (1, 1001, 'Offer Accepted'), (2, 1001, 'Offer Rejected'), (3, 1001, 'No Offer'), (1, 1000, 'No Offer'), (3, 1000, 'Offer Rejected'), (4, 1000, 'Offer Rejected'), (5, 1000, 'Offer Accepted'), (2, 1003, 'Offer Accepted'), (1, 1002, 'Offer Accepted');
学生关系示例数据:
(1000, 'John Smith', 'Electrical Engineering', 3.6)
application(JID INT NOT NULL, SID INT NOT NULL, appStatus VARCHAR(100), PRIMARY KEY (JID, SID);
学生关系students(SID INT PRIMARY KEY, SName VARCHAR(100), Smaj VARCHAR(100), gpa FLOAT);
SELECT SName
FROM students
INNER JOIN applications ON applications.SID = students.SID
GROUP BY students.SName
HAVING COUNT(appStatus = 'Offer Accepted') + COUNT(appStatus = 'Offer Rejected') > 2
答案 0 :(得分:1)
COUNT()仅返回记录数。发生的情况是,相等性检查SELECT SName
FROM students
INNER JOIN applications ON applications.SID = students.SID
GROUP BY students.SName
HAVING SUM(appStatus = 'Offer Accepted') + SUM(appStatus = 'Offer Rejected') > 2
的取值为0或1,具体取决于其是否正确。然后,count函数只是将零和一的 count 相加,而不管其值如何。
您可以使用SUM来实际获得1(即满足此条件的记录数)。
<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "", "nyheter");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
//get results from database
$result = mysqli_query($connection,"SELECT * FROM post");
$all_property = array(); //declare an array for saving property
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf8" />
<link rel="stylesheet" href="nyheter.css" type="text/css" />
<link rel="stylesheet" href="read.css">
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="jquery.dynamicmaxheight.js"></script>
</head>
<body>
<div class="allt-2">
<div class="content">
<img src="http://cdn4.iconfinder.com/data/icons/socialmediaicons_v120/48/google.png"/ alt="" >
<h3><?php echo $Name?></h3>
</div>
<section class="section js-dynamic-height" data-maxheight="150" >
<p class="dynamic-height-wrap"> Hej
</p>
<button class="js-dynamic-show-hide button" title="Läs mer" data-replace-text="Läs mindre">Läs mer</button>
</section>
<img class="ny-img" src="http://placehold.it/500x320"/>
</div>
</body>
<script>
$(document).ready(function(){
$('.js-dynamic-height').dynamicMaxHeight();
});
</script>
</body>
</html>
答案 1 :(得分:0)
这应该有效。
SELECT s.SID, SName, COUNT(*)
FROM application a
JOIN students s ON a.SID = s.SID
WHERE appStatus IN ('Offer Accepted', 'Offer Rejected')
GROUP BY s.SID, SName
HAVING COUNT(*) > 1