473,386 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Howto get multiple wheres on one column

2
Hello,

i have this constellation on tables in my database:

Table A
id|content

Table B
table_a_id|table_c_id

Table C
id|name

My question is:
How to write a select like this:
Select content from table_a inner join table_b on (table_a.id = table_b.table_a_id)
inner join table c on (table_b.table_c_id = table_c.id)
Where name = "nameA" and name = "nameB".

So that i can get that row's from Table A that matches the two names in this n:m constellation. With this Sql-Statement i get of course nothing, with an or i get the row's from Table A with either nameA or nameB.

Thank's for every answer.
Dec 26 '09 #1

✓ answered by nbiswas

Try this

Sample data for TableA

Expand|Select|Wrap|Line Numbers
  1. id1    content
  2. 1    content1
  3. 2    content2
  4. 3    content3
  5. 4    content4
  6. 5    content5
Sample data for TableC

Expand|Select|Wrap|Line Numbers
  1. id2    name
  2. 101    name1
  3. 202    name2
  4. 303    name3
  5. 404    name4
  6. 505    name5
Sample data for TableB

Expand|Select|Wrap|Line Numbers
  1. id1    id2
  2. 1    101
  3. 1    202
  4. 1    303
  5. 1    505
  6. 2    101
  7. 2    404
  8. 3    303
  9. 4    101
  10. 4    202
  11. 4    404
  12. 5    101
Now I want to choose those records from TableA which has got both 'Name1' and 'Name2' in TableC.

I such a case, the answer should be 1 & 4

Query 1

Expand|Select|Wrap|Line Numbers
  1. select a.id1, a.content from tableA a
  2. join (
  3. select b.id1
  4. from tableC c
  5. join tableB b on c.id2 = b.id2 
  6. where c.name = 'name1' or c.name = 'name2' 
  7. group by b.id1 
  8. having COUNT(c.name) > 1) x
  9. on a.id1 = x.id1 
Query 2:

Expand|Select|Wrap|Line Numbers
  1. select a.id1, a.content from @tableA a where a.id1 in(
  2. select b.id1
  3. from @tableC c
  4. join @tableB b on c.id2 = b.id2 
  5. where c.name = 'name1' or c.name = 'name2' 
  6. group by b.id1 
  7. having COUNT(c.name) > 1) 
Output:

Expand|Select|Wrap|Line Numbers
  1. id1    content
  2. 1    content1
  3. 4    content4
Hope this helps.
Let me know in case of any concern.

2 1788
nbiswas
149 100+
Try this

Sample data for TableA

Expand|Select|Wrap|Line Numbers
  1. id1    content
  2. 1    content1
  3. 2    content2
  4. 3    content3
  5. 4    content4
  6. 5    content5
Sample data for TableC

Expand|Select|Wrap|Line Numbers
  1. id2    name
  2. 101    name1
  3. 202    name2
  4. 303    name3
  5. 404    name4
  6. 505    name5
Sample data for TableB

Expand|Select|Wrap|Line Numbers
  1. id1    id2
  2. 1    101
  3. 1    202
  4. 1    303
  5. 1    505
  6. 2    101
  7. 2    404
  8. 3    303
  9. 4    101
  10. 4    202
  11. 4    404
  12. 5    101
Now I want to choose those records from TableA which has got both 'Name1' and 'Name2' in TableC.

I such a case, the answer should be 1 & 4

Query 1

Expand|Select|Wrap|Line Numbers
  1. select a.id1, a.content from tableA a
  2. join (
  3. select b.id1
  4. from tableC c
  5. join tableB b on c.id2 = b.id2 
  6. where c.name = 'name1' or c.name = 'name2' 
  7. group by b.id1 
  8. having COUNT(c.name) > 1) x
  9. on a.id1 = x.id1 
Query 2:

Expand|Select|Wrap|Line Numbers
  1. select a.id1, a.content from @tableA a where a.id1 in(
  2. select b.id1
  3. from @tableC c
  4. join @tableB b on c.id2 = b.id2 
  5. where c.name = 'name1' or c.name = 'name2' 
  6. group by b.id1 
  7. having COUNT(c.name) > 1) 
Output:

Expand|Select|Wrap|Line Numbers
  1. id1    content
  2. 1    content1
  3. 4    content4
Hope this helps.
Let me know in case of any concern.
Dec 26 '09 #2
Idrisu
2
Thank you for the great answer.
After seeing your sql-statements i realize that i'm a total noob on sql ;)

I found also one solution:

Select tableA.id1 from (
Select tableA.id1 as id1A from tableA inner join tableB on (tableA.id1 = tableB.id1) inner join tableC on (tableB.id2 = tableC.id2) where tableC.name = "Name1") as a
inner join (
Select tableA.id1 as id1B from tableA inner join tableB on (tableA.id1 = tableB.id1) inner join tableC on (tableB.id2 = tableC.id2) where tableC.name = "Name2") as b on (id1A = id1B)
... for every tableC.name more i would need one join more

But i will use your's because it's mutch easier and faster.

In your solution it's also helpful to now, that you need to raise the count at COUNT(c.name) for every c.name you need more to match, example:

1. select a.id1, a.content from tableA a
2. join (
3. select b.id1
4. from tableC c
5. join tableB b on c.id2 = b.id2
6. where c.name = 'name1' or c.name = 'name2' or c.name = 'name3'
7. group by b.id1
8. having COUNT(c.name) > 2) x
9. on a.id1 = x.id1
Dec 27 '09 #3

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

Similar topics

7
by: Greg Brunet | last post by:
I'm writing some routines for handling dBASE files. I've got a table (DBF file) object & field object already defined, and after opening the file, I can get the field info like this: >>>...
2
by: Daniel Tan | last post by:
Hi, how can i obtain the value of combobox multiple bound column data ? Eg, if i select combo box, it has more than one field, name and age and id listed. Pls advise, thanks. Regards, Daniel
0
by: Matt | last post by:
Hi all, is there any way to create a multiple line column header within the DataGrid? Thanks, Matt.
1
by: Bob Loveshade | last post by:
I am looking for an example that shows how to select and highlight multiple rows in a DataGrid. My DataGrid is part of a Web User Control which is contained in an ASPX page. I haven't been...
2
by: Sam | last post by:
Hi all I have a database table that has multiple column primary key and every thing looks fine when I load it in the datagrid web control. The problem is that the datagrid web control can only...
0
by: Carlos Barini | last post by:
I need to change the "Column.BackColor" when the user clicks the ColumnHeader. How can I do that? Tks. Carlos Barini. Brazil.
2
by: rise4peace | last post by:
Howdy, I am trying to select a range of data within an excel spreadsheet using a SQL query. The two queries below do the trick but I was wondering if there's any way to combine both of these SQL...
7
by: =?Utf-8?B?QVRT?= | last post by:
HOWTO Run multiple SQL statements from ASP/ADO to an Oracle 10g. Please help, I'm trying to write an ASP page to use ADO to run a long query against an Oracle 10g database, to create tables,...
3
by: rn5a | last post by:
A MS-Access DB table has the following 6 columns - TeacherID, ClassID, VenueID, AvailDate, StartTime & EndTime. The data type of the 1st 3 columns is int whereas the data type of the last 3 columns...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...

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.