[英]Can I have an SQL query where I select all from a table where ID = another query?
I want to be able to filter posts based on what the designers do, I have each one in a Boolean in the designer table ie Logo: 1, Website: 0 The ID is a foreign key which is the same in both tables so I want to show posts based on the data I have in the designer table. 我希望能够根据设计者的工作来过滤帖子,我在设计器表中的布尔值中分别放置了一个,即徽标:1,网站:0 ID是一个外键,在两个表中都相同,所以我想根据我在Designer表中拥有的数据显示帖子。
$sqldesigner = SELECT designerID FROM designers WHERE logo = 1; $ sqldesigner =从设计师的WHERE徽标中选择SELECT designerID = 1;
$sqlall = SELECT * FROM post WHERE designerID = $sqldesigner; $ sqlall = SELECT * FROM WHERE后面的designerID = $ sqldesigner;
I want to show the posts based on the designer who made the post, in this example the designer makes logos so in the designer table when he signed up he ticked a box saying logo to set the logo Boolean to 1, currently I show all posts on a page but I want to have a button to filter these posts to only show posts made by a designer who makes logos, my first query is to get all the IDs of the designers who make logos then my second one was to get all posts from those designers 我想根据发布者的设计师来显示帖子,在此示例中,设计师制作徽标,因此当他签名时在designer表中他勾选了一个框,上面写着徽标,将徽标布尔值设置为1,目前我显示所有帖子在页面上,但我想有一个按钮来过滤这些帖子,以仅显示由徽标设计者撰写的帖子,我的第一个查询是获取徽标设计者的所有ID,然后我的第二个查询就是获取所有帖子从那些设计师那里
Hopefully I gave enough info here and thanks in advance for the help 希望我在这里提供了足够的信息,并在此先感谢您的帮助
You can use the following SQL statement to fulfill your requirements: 您可以使用以下SQL语句满足您的要求:
SELECT * FROM post WHERE id IN (SELECT ID FROM designers WHERE logo = 1);
The SQL IN operator allows you to specify multiple values in a WHERE clause. SQL IN运算符允许您在WHERE子句中指定多个值。 The IN operator is a shorthand for multiple OR conditions. IN运算符是多个OR条件的简写。 More about SQL IN Operator can be found here . 有关SQL IN运算符的更多信息,请参见此处 。
OR 要么
SELECT p.*, d.id FROM post p,designers d WHERE p.id = d.id AND d.logo = 1;
What this does, is that it helps you to rename post as p and designers as d and then apply your where conditions based on two different tables. 这样做是为了帮助您将post重命名为p,将设计者重命名为d,然后根据两个不同的表应用where条件。
BOTH the above queries can be used to solve your problem. 以上查询均可用于解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.