473,472 Members | 2,247 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Select query between two tables

539 Contributor
I need help on how to query this scenario

Suppose i have this tables:

TABLE1
+--------+-------+
| usr | value |
+--------+-------+
| werty1 | 789 |
| werty2 | 789 |
| werty3 | 1111 |
| werty4 | 3423 |
| werty5 | 234 |
| werty6 | 234 |
+--------+-------+

TABLE2
+--------+-------+
| usr | value |
+--------+-------+
| werty1 | 789 |
| werty2 | 789 |
| werty3 | 3423 |
| werty4 | 3423 |
| werty5 | 222 |
| werty6 | 333 |
+--------+-------+

I would like to get the result only to those usr and exist both on TABLE1 and TABLE2 that their value is different.

the expected output:

werty3 11111 3423
werty5 234 222
werty6 234 333

I still get stuck on how to query this stuff and as of now, im experimenting on this query hoping to gain new

Expand|Select|Wrap|Line Numbers
  1. select usr,value from TABLE1 where usr in (select usr from TABLE2) and value in (select values from TABLE2);
Any advise?
Jan 8 '10 #1
4 3558
SLauren
60 New Member
You can try this:

Expand|Select|Wrap|Line Numbers
  1. SELECT    DISTINCT TABLE1.*
  2. FROM        TABLE1
  3. LEFT JOIN     TABLE2
  4. ON        TABLE1.usr = TABLE2.usr AND TABLE1.value = TABLE2.value WHERE TABLE2.usr is null AND TABLE2.value IS NULL;
Hope this could help.

Thanks,
Lauren
Jan 8 '10 #2
mwasif
802 Recognized Expert Contributor
Try the following
Expand|Select|Wrap|Line Numbers
  1. SELECT usr,value FROM TABLE1 
  2. INNER JOIN TABLE2 ON TABLE1.usr = TABLE2.usr
  3. WHERE TABLE1.value <> TABLE2.value
Jan 8 '10 #3
nbiswas
149 New Member
Apart from the answers given here, you can also try the following queries

Query 1:
Expand|Select|Wrap|Line Numbers
  1. select t1.usr, t1.value, t2.value
  2. from TABLE1  t1 
  3. join TABLE2 t2
  4. on t1.value  <> t2.value and t1.usr  = t2.usr 
Query2

Expand|Select|Wrap|Line Numbers
  1. select t1.usr, t1.value, t2.value
  2. from TABLE1  t1 , TABLE2 t2
  3. where t1.value  <> t2.value and t1.usr  = t2.usr
Query3
Expand|Select|Wrap|Line Numbers
  1. select  t1.usr, t1.value,t2.value from TABLE1  t1 , TABLE2 t2
  2. where t1.value not in (select value from TABLE2)
  3. and t1.usr  = t2.usr 
Query 4

Expand|Select|Wrap|Line Numbers
  1. select * from (
  2. select  t1.usr, t1.value,
  3.     (select t2.value from  TABLE2 t2
  4.      where t1.value <> t2.value and t1.usr  = t2.usr) value2
  5. from TABLE1  t1 )x where x.value2 is not null
  6.  
Output(For all the cases)

Expand|Select|Wrap|Line Numbers
  1. usr    value    value
  2. werty3    1111    3423
  3. werty5    234    222
  4. werty6    234    333
Jan 9 '10 #4
sukatoa
539 Contributor
All of your replies are effective, thank you so much. appreciated :)
Jan 11 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Craig | last post by:
Acces 2000: I have several 'lookup' type tables with a standard field naming convention of id(pk) and name in a medium sized database. When I create a select query involving 3 or 4 of these tables...
1
by: septen | last post by:
Hi, I have a select query of 12 tables. The SQL code is as follows: SELECT OptChannelRangeAve.ChannelRangeMin, OptChannelRangeAve.ChannelRangeMax, OptChannelRangeAve.SampleAveraging,...
5
by: parwal.sandeep | last post by:
Hello grp! i'm using INNODB tables which are using frequently . if i fire a SELECT query which fetch major part of table it usually take 10-20 seconds to complete. in mean time if any UPDATE...
0
by: djflow | last post by:
Hi! II was wondering if you can help me with SQL query.. Below 7 separated select query works fine(only when they are retrieved separately) But I want to combined them together and so that i...
1
by: wizardRahl | last post by:
Hello, I'm curious to know if there is a way to select all tables when using a query in access. I have some (unsuccessful) cascading combo boxes and need a little help maybe working out the...
8
by: Trevor2007 | last post by:
I have this select query : SELECT DISTINCT ReferredToLU.ReferredTo FROM ReferredToLU ORDER BY ReferredToLU.ReferredTo; I would like to do two thing but not sure if they are possable or how to do...
5
by: MARIEDB2 | last post by:
Hello, I am struggling to build a query on a DB2 db wich does an update for multiple fields which are the result of a select query with where clause (using multiple tables in the condition). ...
6
by: padmaneha | last post by:
Hi I have created two tables which are movies table and videos table Columns of movies table are movie id, movietitle, moviereviewid and the select statement which I have written to fetch...
0
by: padmaneha | last post by:
Hi Thanks for your response but the query which you gave in my previous post doesnt work. It displays the same cnt for movie reviews and videoreviews Let me explain you in detail As I...
30
by: iheartvba | last post by:
Hi, I already have 3 Databases running: A. they all have the same tables and the same structure B. There is no 1 Master table they are all separate tables What I want to do is to merge them...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.