Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/124.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么我的mysql搜索查询没有返回任何内容?_Mysql_Sql - Fatal编程技术网

为什么我的mysql搜索查询没有返回任何内容?

为什么我的mysql搜索查询没有返回任何内容?,mysql,sql,Mysql,Sql,我有以下几张表 posts表格 create table posts (id int, calories int, INGREDIENTS INT, SERVE INT); INSERT INTO POSTS VALUES(1,100,5,1); INSERT INTO POSTS VALUES(2,100,5,1); CREATE TABLE post_translations (id int, post_id int, language_id int, `group` varchar(50

我有以下几张表

posts
表格

create table posts (id int, calories int, INGREDIENTS INT, SERVE INT);
INSERT INTO POSTS VALUES(1,100,5,1);
INSERT INTO POSTS VALUES(2,100,5,1);
CREATE TABLE post_translations (id int, post_id int, language_id int, `group` varchar(50), `key` varchar(50), `value` varchar(50));
insert into post_translations  values (2,1,1, 'title_group','post_tagline','spicy and tasty chicken');
insert into post_translations  values (6,2,1, 'title_group','post_tagline','spicy and tasty mutton');
create table post_tags(id int, post_id int, tag varchar(50));
insert into post_tags values(7,1,'chicken');
post_翻译
表格

create table posts (id int, calories int, INGREDIENTS INT, SERVE INT);
INSERT INTO POSTS VALUES(1,100,5,1);
INSERT INTO POSTS VALUES(2,100,5,1);
CREATE TABLE post_translations (id int, post_id int, language_id int, `group` varchar(50), `key` varchar(50), `value` varchar(50));
insert into post_translations  values (2,1,1, 'title_group','post_tagline','spicy and tasty chicken');
insert into post_translations  values (6,2,1, 'title_group','post_tagline','spicy and tasty mutton');
create table post_tags(id int, post_id int, tag varchar(50));
insert into post_tags values(7,1,'chicken');
post_标签
表格

create table posts (id int, calories int, INGREDIENTS INT, SERVE INT);
INSERT INTO POSTS VALUES(1,100,5,1);
INSERT INTO POSTS VALUES(2,100,5,1);
CREATE TABLE post_translations (id int, post_id int, language_id int, `group` varchar(50), `key` varchar(50), `value` varchar(50));
insert into post_translations  values (2,1,1, 'title_group','post_tagline','spicy and tasty chicken');
insert into post_translations  values (6,2,1, 'title_group','post_tagline','spicy and tasty mutton');
create table post_tags(id int, post_id int, tag varchar(50));
insert into post_tags values(7,1,'chicken');
我正在尝试以下查询,以搜索应该与
post\u翻译
post\u标签

SELECT  posts.id, posts.`image`
FROM posts
JOIN post_translations ON  post_translations.`post_id` = posts.`id`
JOIN post_tags ON  post_tags.post_id = posts.id
WHERE post_tags.`tag` 
LIKE '%spicy%' 
OR post_translations.`value` 
LIKE '%spicy%' GROUP BY posts.id
但它没有返回任何内容,也没有错误。我还想找到一种方法,进一步过滤同一查询中的记录。我还有一张表是
posts\u country

create table posts_country (id int, post_id int, country int);
insert into posts_country values (2,1,2);
insert into posts_country values (3,1,3);
insert into posts_country values (4,2,2);
insert into posts_country values (5,2,3);
我想通过国家id进一步过滤帖子,就像下面的查询返回特定国家的帖子一样

SELECT posts.id, posts.`image` 
FROM posts
WHERE id IN(
      SELECT post_id FROM posts_country WHERE country_id = 1 GROUP BY post_id
) 

能有人帮忙吗,我很感激。非常感谢。

您正在加入所有的表格。因此,只返回满足所有条件的行post_标签表格只有一个贴子,并且标签不是辣的。所以不会从这个表中选择任何行,这就是为什么查询不返回任何内容。 您可以使用左联接而不是联接。或者您也可以尝试以下查询:

  SELECT  posts.*
    FROM posts
    where (id in (select post_id from post_translations where post_translations.value
    LIKE '%spicy%' )
    or id in (select post_id from post_tags where tag LIKE '%spicy%' ))
and id IN(
      SELECT post_id FROM posts_country WHERE country_id = 2 )

这将返回所有需要的行。现在,如果您想使用group,那么您必须在选择列表中使用一些聚合。

请参阅查看@nbk,我将在下次使用。谢谢you@cartli为了更好地理解问题,请以文本格式与表结构共享示例数据,而不是图像。如果您共享db fiddle link或类似的东西会更好。逐个收回查询中的条件,直到获得所需的结果。这是我发现查询的哪一部分未按预期工作的常用方法。然后从这里开始进行故障排除。关于你所得到的反对票,你必须明白,需要帮助和提供帮助的社区也是如此。流量非常高,因此如果无法再现/复制您的情况,大多数人不会费心去帮助您。简言之,如果是你,你愿意从照片中重新键入代码/数据还是从问题中复制并粘贴它们?谢谢你的回答,但这样我会得到重复的记录,我想得到唯一的记录。帖子是否有重复的id?我认为ID是主键。我的意思是在post translation中会有许多正确的ID和post标记,感谢您的回答+1 post_translation或post_标记将有多行,但查询将仅从posts表返回行,其中不应有重复记录。例如,如果post_标记或post_翻译为特定post_id返回多行,则此查询最终将为该post_id表单posts表返回一行。您能再帮我一个忙吗?我如何进一步过滤它,例如,如果我需要属于
国家的记录\u id=2
。你可以阅读我的问题以供参考。