用于搜索两个表的Mysql查询结构

发布于 2024-11-07 14:47:44 字数 828 浏览 0 评论 0原文

我有两个表,结构如下:

Posts Table
id
content

Meta Table
id
post_id
content
name

posts 表是主要父级,可以将元表中的数据与其连接

Post Table
id   content
1    My first row

Meta Table
id   post_id  content   name
4    1        2011-5-5    date
5    1        My Heading  heading
6    2        2012-3-3    date
7    2        My Title    heading

我将如何编写查询来搜索 posts 表中包含 2011-5 元数据内容的条目-5 带有日期名称,我的标题带有标题名称,

我可以单独获取它们

Select *
from posts, meta
WHERE post_id = posts.id
   AND (name='date' AND content='2011-5-5')

但是一旦我添加第二个条件,它就会返回零结果,因为我们要求名称字段是两个不同的东西

Select *
from posts, meta
WHERE post_id = posts.id
   AND (name='date' AND content='2011-5-5') 
   AND (name='heading' AND content='My Heading')

任何帮助都会不胜感激,谢谢!

I have two tables sturctured like so:

Posts Table
id
content

Meta Table
id
post_id
content
name

The posts table is the main parent that can have data from the meta table joined with it

Post Table
id   content
1    My first row

Meta Table
id   post_id  content   name
4    1        2011-5-5    date
5    1        My Heading  heading
6    2        2012-3-3    date
7    2        My Title    heading

How would I write a query to search for an entry in the posts table that contains meta data content of 2011-5-5 with a name of date and My Heading with a name of heading

I can get them individually

Select *
from posts, meta
WHERE post_id = posts.id
   AND (name='date' AND content='2011-5-5')

But as soon as I add a second criteria, it returns zero results, becuase we are asking the name field to be two different things

Select *
from posts, meta
WHERE post_id = posts.id
   AND (name='date' AND content='2011-5-5') 
   AND (name='heading' AND content='My Heading')

Any help would be greatly appreciated, Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

惟欲睡 2024-11-14 14:47:44

您需要加入该表两次:

SELECT *
FROM posts, meta m1, meta m2
WHERE posts.id=m1.post_id
AND posts.id=m2.post_id
AND m1.name="date" AND m1.content="2011-5-5"
AND m2.name="heading" AND m2.content="My Heading"

You'll need to join to the table twice:

SELECT *
FROM posts, meta m1, meta m2
WHERE posts.id=m1.post_id
AND posts.id=m2.post_id
AND m1.name="date" AND m1.content="2011-5-5"
AND m2.name="heading" AND m2.content="My Heading"
陪我终i 2024-11-14 14:47:44

您需要加入元表两次(每次搜索一次) - 像这样:

Select * 
from posts p 
 join meta m_date on m_date.post_id = p.id AND m_date.name='date'
 join meta m_heading on m_heading.post_id = p.id AND m_heading.name='heading'
where
 m_date.content = '2011-5-5' and
 m_heading.content = 'My Heading'

You need to join to the meta table twice (once for each search) - like this:

Select * 
from posts p 
 join meta m_date on m_date.post_id = p.id AND m_date.name='date'
 join meta m_heading on m_heading.post_id = p.id AND m_heading.name='heading'
where
 m_date.content = '2011-5-5' and
 m_heading.content = 'My Heading'
手长情犹 2024-11-14 14:47:44

您是否尝试过:

Select *
from posts, meta, meta meta_2
WHERE (meta.post_id = posts.id
      and meta_2.post_id = posts.id)
   AND ((meta.name='date' AND meta.content='2011-5-5') 
       and (meta_2.name='heading' AND meta_2.content='My Heading'))

由于您想要将两个不同的 meta 记录与单个 post 记录关联,因此您必须从 meta 中进行选择两次(一次用于“日期”元值,一次用于“标题”元值)。

Have you tried this:

Select *
from posts, meta, meta meta_2
WHERE (meta.post_id = posts.id
      and meta_2.post_id = posts.id)
   AND ((meta.name='date' AND meta.content='2011-5-5') 
       and (meta_2.name='heading' AND meta_2.content='My Heading'))

Since there are two different meta records that you want to associate with a single post record, you have to select from meta twice (once for the 'date' meta-value and once for the 'heading' meta-value).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文