简体   繁体   中英

How to sort by sql query?

Ok I have the following SQL Query:

  • Points : id, special, user, points, book, type, date.
  • Videos : id, title, points, src, photo, token, special
  • Book : id, title, image, end_date, token, token_id, author. description

Each video has a special id. The points will tell you which video you watched based on the special matching on points and videos

Each book has a token . The video is told which video to go on based on the token .

--

What I want to do? I want to have a page that displays the point value aa user has used.

在此处输入图片说明

I have tried to do this code:

$one = $db->query("SELECT * FROM points WHERE user='$user' GROUP BY book");

while($fetch_data = mysqli_fetch_array($one)) {
$special = $fetch_data["special"]; 
$points = $fetch_data["points"]; 
$type = $fetch_data["type"]; 
$date = $fetch_data["date"]; 

  // FIND BOOK
$two = $db->query("SELECT * FROM videos WHERE special='$special'"); 

while($fetch_data2 = mysqli_fetch_array($two)) {
 $title = $fetch_data2["title"]; 

 // At this point echo output
}

}

Use inner join to fetch the data from videos and points and loop through the result

$query = $db->query("Select a.*,b.* from points a inner join videos b on a.special = b.special where a.user = '$user'")

while ($result = mysqli_fetch_array($query){
//echo the output
}

use INNER JOIN no need multiple query

"SELECT p.*,v.* FROM points as p 
 inner join videos as v 
 on v.special =p.special 
 WHERE p.user='".$user."' 
 group by p.books"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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