如何从多个表中搜索父内容?

时间:2017-10-23 17:45:19

标签: mysql

表 - 父母

+----+----------+--------------+----------+
| id | user_id  | content_type |  name    |
+----+----------+--------------+----------+
|  1 | 10       | 1            | name - 1 |
|  2 | 12       | 2            | name - 2 |
+----+----------+--------------+----------+

content_type可以是

1 = Single part(movies)
2 = Multi prt(serials, episode)

表 - 孩子

+----+------------+--------------+--------------+-----------+
| id | parent_id  | is_episode   | episode_name | file_name |
+----+------------+--------------+--------------+-----------+
|  1 | 1          | 0            | NULL         | movie.mp4 |
|  2 | 2          | 0            | NULL         | zee.mp4   |
|  3 | 2          | 1            | cname-2      | lil-1.mp4 |
|  4 | 2          | 1            | child-2      | lil-2.mp4 |
+----+------------+--------------+--------------+-----------+

is_episode可以是:

0 = No
1 = Yes

注意:我跳过的其他一些字段

我的查询:

SELECT F.id,F.user_id,F.content_type,F.name,M.is_episode, M.episode_name, M.file_name 
FROM childs M,parents F 
WHERE  M.parent_id = F.id AND M.is_episode=0 AND 
(F.name LIKE '%child%' OR M.episode_name LIKE '%child%' ) 

Abve MySQL查询我得到0记录。有1个内容

我的搜索字符串是基于parents.namechilds.episode_name应用的。我想获取is_episode=0的所有记录。如果我搜索child-2,那么找出我当前记录的父内容。这是我的输出应该来

+----+----------+----------------+----------+------------+--------------+-----------+
| id | user_id  | content_type   | name     | is_episode | episode_name | file_name |
+----+----------+----------------+----------+------------+--------------+-----------+
|  2 | 12       | 2              | name - 2 | 0          | NULL         | zee.mp4   |
+----+----------+----------------+----------+------------+--------------+-----------+

输出:

In above my  two tables my output should come following criterias

如果我按name LIKE '%name%'搜索输出应为

+----+----------+----------------+----------+------------+--------------+
| id | user_id  | content_type   | name     | is_episode | episode_name |
+----+----------+----------------+----------+------------+--------------+
|  1 | 10       | 1              | name - 1 | 0          | NULL         |
|  2 | 12       | 2              | name - 2 | 0          | NULL         |
+----+----------+----------------+----------+------------+--------------+

如果我按name LIKE '%cname%'name LIKE '%name - 2%'name LIKE '%child%'搜索输出应为

+----+----------+----------------+----------+------------+--------------+
| id | user_id  | content_type   | name     | is_episode | episode_name |
+----+----------+----------------+----------+------------+--------------+
|  2 | 12       | 2              | name - 2 | 0          | NULL         |
+----+----------+----------------+----------+------------+--------------+

如果我按name LIKE '%name - 1%'搜索输出应为

+----+----------+----------------+----------+------------+--------------+
| id | user_id  | content_type   | name     | is_episode | episode_name |
+----+----------+----------------+----------+------------+--------------+
|  1 | 10       | 1              | name - 1 | 0          | NULL         |
+----+----------+----------------+----------+------------+--------------+

如果我按name LIKE '%child%'搜索输出应为

+----+----------+----------------+----------+------------+--------------+
| id | user_id  | content_type   | name     | is_episode | episode_name |
+----+----------+----------------+----------+------------+--------------+
|  2 | 12       | 2              | name - 2 | 0          | NULL         |
+----+----------+----------------+----------+------------+--------------+

我无法获得正确的查询输出。你能不能写下满足我所有输出的查询。

1 个答案:

答案 0 :(得分:0)

您的问题是,您要过滤掉Is_episode = 0 行,但是您需要搜索is_episode = 1之类的行cname-2child-2

由于is_episode = 0导致您的问题,删除它并且它应该有效。如果您还有问题,请发表评论。

更新:根据我的理解,根据我的理解,我认为您可能需要基于queries或{{1}的多种table和分隔。然后logic/conditions结果在一起。

union