简体   繁体   English

条件在2个表中,但仅从一个表中选择

[英]condition in 2 table but select just from one table

I have two tables in a mysql database. 我在mysql数据库中有两个表。

I need to select columns just from the first table and do the condition in the 2 table for example : 我只需要从第一个表中选择列,然后在2个表中做条件,例如:

I have in the first table the columns: 我在第一张表中的列:

amount | date | name | address

and in the second I have: 在第二个中我有:

amount | date | cin | time 

The condition would be WHERE amount = amount and date = date . 条件为: WHERE amount = amount and date = date

But select just the data from the first table. 但是,仅从第一个表中选择数据。 I dont need to display the data of the second table. 我不需要显示第二个表的数据。

You could use the EXISTS operator to find out whether a corresponding row in the second table exists: 您可以使用EXISTS运算符找出第二个表中是否存在对应的行:

SELECT * FROM first_table t1
WHERE EXISTS (
    SELECT 1 
    FROM second_table t2
    WHERE
        t1.amount = t2.amount AND t1.date = t2.date
);

This ensures you won't have to use DISTINCT to reduce your resultset, if more than one row with your condition exists in the second table. 如果第二个表中存在符合条件的一行以上,则可以确保您不必使用DISTINCT来减少结果集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Laravel-从数据透视表中仅选择一行并返回数据 - Laravel - Select just one row from pivot table and return data 从一个条件为的表中检索数据,并在一个SELECT查询中检查另一个表中的记录 - Retrieve data from a table with a condition where and check the record in another table in one SELECT query PHP / SQL:仅使用一个查询,如果两个表中都有数据,则从两个表中选择行;否则,仅从一个表中进行SELECT - PHP/SQL: Using only one query, SELECT rows from two tables if data is in both tables, or just SELECT from one table if not 如何从两个条件的表中选择数据 - How to select data from table with two condition 如何从另一个表中选择表中的特定条件 - how to select a specific condition in table from another table MySQL联接:从子表中选择一行满足条件,否则为NULL - MySQL JOINS: Select one row from child table is condition is met, otherwise NULL 如何根据一个或多个条件从表中选择数据? - How can I select data from table based on one or multiple condition? 从一个表中选择行,但语句条件在另一个表中 - Pick rows from one table but where statement condition is in another table 仅将新行从一张表上传到另一张表 - upload just new rows from one table to another one 从一个小表和一个大表中优化选择查询 - optimizing select query from one small table and one large table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM