简体   繁体   English

如何连接两个表以形成SQL查询?

[英]How to connect two tables to form an SQL query?

Ok, I have two tables: points and videos . 好的,我有两张桌子: 视频 I only want a video to show if the logged in user hasn't already been awarded points for watching that particular video. 我只想要一个视频来显示登录用户是否还没有获得观看该特定视频的积分。

The points db structure: id, special, user and points. points db结构:id,special,user和points。

Special is the unique id of the video. 特别是视频的唯一ID。

The videos db structure: id, title, points, src, photo, token, special. videos db结构:id,title,points,src,photo,token,special。

There are multiple pages on this site. 此网站上有多个页面。 Token tells the video which page that video is ssigned to. 令牌告诉视频视频被指定的页面。 Special is a special id for only that video and correlates to the special in the points db Special是仅适用于该视频的特殊ID,并且与points db的特殊关联相关

-- -

This is the SQL Query I basicially want to say: $db->query("SELECT * FROM videos WHERE token='$id' AND num_rows OF user='$user' AND special='$special' == 0 FROM TABLE points"); 这是SQL查询我基本上想说的: $db->query("SELECT * FROM videos WHERE token='$id' AND num_rows OF user='$user' AND special='$special' == 0 FROM TABLE points");

Now I know this is not the proper format to write SQL Queries, but this is what I want in human. 现在我知道这不是编写SQL查询的正确格式,但这是我想要的人类。 How do I translate this request to SQL? 如何将此请求转换为SQL?

You just need to join videos, points tables using the special column. 您只需要使用特殊列加入视频,点表。

select count(*) from videos a join points b on (a.special = b.special) where b.user = '$user' and a.token = '$id';

This count will be 0 if the user has not watched the video. 如果用户没有观看视频,则此计数将为0。

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

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