[英]MySQL JOIN not working
I am trying to complete a MySQL join from one table to another but it is not working. 我正在尝试从一个表到另一个表完成MySQL连接,但它无法正常工作。 Ideally, I want to be able to do with with prepared statements (as it is more secure) but for now I am just trying to get this to work normally.
理想情况下,我希望能够处理准备好的语句(因为它更安全)但是现在我只是想让它正常工作。
Here is my database setup: 这是我的数据库设置:
Posts: 帖子:
id | title | text
1 | Evening | Something here...
2 | Trip away | Bets place to go...
Tags: 标签:
post_id | tag
1 | weather
1 | Autumn
2 | Holidays
This is my attempt so far: 这是我到目前为止的尝试:
$tag = mysqli_real_escape_string($_GET['tag']);
$select = mysqli_query($mysqli, "SELECT posts.id, posts.title FROM posts INNER JOIN tags ON tags.tag = ".$tag);
However this does not work. 但是这不起作用。 What I am trying to do is select all the posts with the relevant tag that has been searched for and then output this to the user.
我要做的是选择所有已搜索相关标签的帖子,然后将其输出给用户。 Please help I am really stuck
请帮助我真的卡住了
Edit: While loop the outputted data 编辑:循环输出数据
$select = mysqli_query($mysqli, "SELECT posts.id, posts.title FROM posts INNER JOIN tags ON tags.post_id = posts.id WHERE tags.tag = ".$tag);
while($row = mysqli_fetch_array($select)) {
echo print_r($row);
}
your inner join syntax is incorrect 您的内部联接语法不正确
SELECT posts.id, posts.title
FROM posts
INNER JOIN tags ON tags.post_id = posts.id
WHERE tags.tag = "some_tag_here"
you need to join the two tables together and then use a WHERE to filter by tag 您需要将两个表连接在一起,然后使用WHERE按标记过滤
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.