简体   繁体   English

我可以从一个ID =另一个查询的表中选择全部的SQL查询吗?

[英]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.

相关问题 Laravel查询构建器-选择另一个表具有其ID的所有位置 - Laravel query builder - Select all where another table has their ID 使用另一个表上的WHERE子句从表中选择的SQL查询 - SQL Query to select from a table using a WHERE clause on another table 从另一个查询没有返回结果的表中选择所有 - Select all from table where another query retuns no results mysql查询-仅从另一个表中选择id = id,而该表中的字段=值 - mysql query - only select where id = id from another table and a field in that table = a value 我可以忽略另一个SQL查询中的where子句吗 - Can I ignore a where clause in another sql query 从ID不在其他查询中的房间中选择ID +(日期比较) - Select ID from rooms where ID not in another query + (Date compare) 如何从表 WHERE id=variable 中选择字段? - How can I SELECT field FROM table WHERE id=variable? 如何将这些查询与另一个父表中的 where 子句组合成单个查询? - How can i combine these queries into single query with where clause from another parent table? Codeigniter - 选择id不在的位置(另一个查询结果) - Codeigniter - select where id not in (another query result) 如何创建SQL查询,我可以使用WHERE&AND子句选择一列的最大数量 - How to create an SQL query where I can select the max number of one column with an WHERE & AND clause
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM