我有两张桌子:
这是我的posts
表:
+-----+----------------+-------------+
| ID | post_title | post_status |
+-----+----------------+-------------+
| 105 | Red Shirt | active |
| 106 | Green Shoes | active |
| 107 | Blue Hat | hidden |
+-----+----------------+-------------+
这是我的meta
表:
+-----------+------+
| object_id | lang |
+-----------+------+
| 105 | en |
| 106 | fr |
| 107 | en |
+-----------+------+
我正在尝试编写一个SQL查询,该查询返回posts
post_status
active
和lang
en
的所有105
的ID。使用这两个表,应该只有一个结果(ID
)。
我无法将object_id
与var switchTab = function() {
var p = $(this).parent('li');
var i = p.index();
var s = section.eq(i);
var c = s.find('*');
section.removeClass('active');
sectionContent.removeAttr('style');
s.addClass('active');
c.css({
transform: 'none',
opacity: 1
});
linkParent.removeClass('active');
p.addClass('active');
return false;
};
link.on('click', switchTab);
</script>
匹配。非常感谢任何帮助,谢谢。
答案 0 :(得分:0)
SELECT p.ID
FROM posts p, meta m
WHERE p.ID = m.object_id
AND p.post_status = 'active'
AND m.lang='en'
答案 1 :(得分:0)
加入两个表,然后指定条件:
SELECT posts.ID
FROM posts
INNER JOIN meta ON meta.object_id = posts.id
WHERE posts.post_status = 'active'
AND meta.lang = 'en'
答案 2 :(得分:0)
鉴于您只想要那些在两个表中都有记录的行,您需要一个INNER JOIN
。这里有关于不同联接类型的好帖子:SQL JOIN and different types of JOINs
对于您的查询,您需要以下内容:
SELECT
PostTbl.ID -- The column(s) you want to select
FROM
`posts` AS PostTbl -- I prefer aliasing every table so I keep the habit when self joining
INNER JOIN -- Since we want only the rows with records in both tables
`meta` AS MetaTbl
ON PostTbl.ID = MetaTbl.object_id -- The conditions on which we want the tables to join
WHERE
PostTbl.post_status = 'active' -- Post is 'active'
AND MetaTbl.lang = 'en'; -- Language is 'en'