如何在sql中显示来自2个或更多表的数据 [英] how to show data from 2 or more tables in sql

查看:54
本文介绍了如何在sql中显示来自2个或更多表的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从2个表中获取数据,但它显示了一个duplicat记录在一个表中只有153个记录,但是从2个表中取出后它给出了大约256个记录,哪里有问题我的查询是什么?
< br $> b $ b

i am fetching data from 2 tables but it showing a duplicat record In one table there is only 153 record but after fetching from 2 tables it gives around 256 record where is problem my query is


select a.city,a.locality,a.ageofconstruction,a.furnished,a.rdbprofile,dc.locality from Ads as a inner join DelhiCentral as dc on dc.city=a.city  where dc.city='DelhiCentral' order by city







所以请一些人告诉.....




so pls some one tell.....

推荐答案

尝试:

Try:
SELECT DISTINCT
      a.city
    , a.locality
    , a.ageofconstruction
    , a.furnished
    , a.rdbprofile
    , dc.locality 
FROM Ads AS a 
INNER JOIN 
(
SELECT DISTINCT
     city
   , locality
FROM DelhiCentral
)AS dc 
ON dc.city=a.city  
WHERE a.city='DelhiCentral' 
ORDER BY a.city


假设您在DelhiCentral中有一行其中city colu mn包含DelhiCentral,广告中有两行,其中city列包含DelhiCentral,然后内部联接始终生成两行。这就是内部联接的工作方式,它们将根据相等的字段/列来考虑公共数据行。



您已经声明在表中只有153条记录,但是第二个表可能有更多的行而不是256条,但该表中只有256行满足条件city匹配第一个表中保存的那个。





问候

Pawan
Suppose you have one row in DelhiCentral where city column contains "DelhiCentral" and there are two rows in Ads where city column contains "DelhiCentral" then the inner join always produces two rows. This is how inner joins work they will take in to account the common data rows based on the fields/columns that are compared for equality.

You have stated that in table there are only 153 records however the second table might have more rows than even 256 but only 256 rows in that table fulfill the criteria where the city matches the one saved in first table.


Regards
Pawan

这篇关于如何在sql中显示来自2个或更多表的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆