如何通过投票订购参赛作品?

时间:2016-09-20 08:23:57

标签: mysql

这些是我的表格:

CREATE TABLE IF NOT EXISTS `entries` (
  `id` int NOT NULL AUTO_INCREMENT,
  `title` text,
  `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;


id title date
1  aaa   2016-09-15 19:00:00
2  bbb   2016-09-14 19:00:00
3  ccc   2016-09-13 19:00:00
4  ddd   2016-09-12 19:00:00
5  eee   2016-09-11 19:00:00


CREATE TABLE IF NOT EXISTS `votes` (
  `id` int NOT NULL AUTO_INCREMENT,
  `id_entry` int,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

id id_entry
1  1
2  1 
3  1
4  2
5  3

我需要一种方法来通过投票然后按日期来订购所有条目,所以我写了这个查询:

SELECT COUNT(entries.id) as totvotes, entries.id, entries.date FROM entries LEFT JOIN votes ON entries.id=votes.id_entry GROUP by entries.id ORDER by totvotes DESC, entries.date DESC

结果如下:

totvotes entries.id entries.date
3        1          2016-09-15 19:00:00
1        2          2016-09-15 19:00:00
1        3          2016-09-15 19:00:00
1        4          2016-09-15 19:00:00
1        5          2016-09-15 19:00:00

如您所见,对于没有投票的条目(4和5),查询显示totvotes = 1。我怎么能避免这种情况?

1 个答案:

答案 0 :(得分:1)

只需计算<h1>WELCOME TO TOILERT</h1></P> <?php $servername = "localhost"; $username = "toilert"; $password = "toilert"; $dbname = "cleaning_db"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } echo "Connected successfully"; //http://localhost/toilet-backend/backend.php?dirty=1&cubicleID=10003 //GET the data from android /* $dirty = $_GET["dirty"]; //1 for true . 0 for false $emergency = $_GET["emergency"]; //1 for true . 0 for false $faulty = $_GET["faulty"]; //1 for true . 0 for false */ $issue = htmlspecialchars($_GET["issue"]); // Sanitize input with htmlspecialchars $cubicleID = $_GET['cubicleID']; //Check if the request has been sent before //ensure no duplicate toilet data $query = "SELECT * FROM cleaner_request WHERE cubicle_id = $cubicleID"; $result = $conn->query($query); if ($result->num_rows > 0) { $json = array("status" => 200, "msg" => "Complaint has already been created"); echo json_encode($json); } else { switch ($issue) { case "dirty": case "emergency": case "faulty": $query = "INSERT INTO cleaner_request (cubicle_id, $issue) VALUES ('$cubicleID', '1')"; break; default: $error = "Invalid status"; } if (empty($error)) { $result = $conn->query($query); echo "Data written to Database"; $json = array("status" => 200, "msg" => "Complaint has been successfully created"); } else { echo "error" ; //$json = array("status" => 418, "msg" => $error); } echo json_encode($json); /* Ben commented this out. Because code if ($dirty == 1) { //dirty $query = "INSERT INTO cleaner_request (cubicle_id,dirty) VALUES ('$cubicleID','$dirty')"; $result = $conn->query($query); $json = array("status" => 200, "msg" => "Complaint has been successfully created"); echo json_encode($json); } else if ($dirty == 0) { //clean } */ //check require location require 'C:/xampp/htdocs/toilet-backend/twilio-php-master/Services/Twilio.php'; $AccountSid = "AC52a80f076cca00c9547fdfd17bf4fbbb"; $AuthToken = "b145c2d4f7b722fe79cbe38f3009afcc"; // Step 3: instantiate a new Twilio Rest Client $client = new Services_Twilio($AccountSid, $AuthToken); $client->account->messages->create(array( 'To' => "+65 9067 9919", 'From' => "+15672420325", 'Body' => "Greetings, please clean up Cubicle 100 001 and 100 004. Thank you. This service is brought to you by Toilert.", )); mysqli_close($conn); //$conn->close(); } ?> 而不是此votes.id_entry

所以试试这个:SELECT COUNT(entries.id)

关于SELECT COUNT(votes.id_entry) as totvotes...的一些细微之处:

COUNT

SQL FIDDLE