Geekzone: technology news, blogs, forums
Guest
Welcome Guest.
You haven't logged in yet. If you don't have an account you can register now.


620 posts

Ultimate Geek
+1 received by user: 54

Subscriber

Topic # 123303 2-Jul-2013 22:00
Send private message

Before anyone asks, this isn't homework, its a potential aid to an unfortunately very real problem :D

I have a query which works fine that goes:
select stuff from table1 where id1 = 'string_id1' and id2 = 'string'

My problem is I have the values for string_id1 and string_id2 in another table i.e table2.  So to be clear, table2 consists of 2 columns called string_id1 and string_id2.

I want the results of the query for each line in table2.  What's the best way to do this?

Filter this topic showing only the reply marked as answer Create new topic
4005 posts

Uber Geek
+1 received by user: 698

Trusted
Subscriber

  Reply # 848435 2-Jul-2013 22:11
One person supports this post
Send private message

I have a query which works fine that goes:
select stuff from table1 where id1 = 'string_id1' and id2 = 'string'

My problem is I have the values for string_id1 and string_id2 in another table i.e table2. So to be clear, table2 consists of 2 columns called string_id1 and string_id2.

I want the results of the query for each line in table2. What's the best way to do this?


If I'm understanding the problem correctly then you could try:

SELECT stuff FROM table1
WHERE id1 IN (SELECT string_id1 FROM table2)
AND id2 IN (SELECT string_id2 FROM table2)

3165 posts

Uber Geek
+1 received by user: 890

Trusted

  Reply # 848438 2-Jul-2013 22:23
Send private message

Or just use a join:

http://sqlfiddle.com/#!6/c006c/1



620 posts

Ultimate Geek
+1 received by user: 54

Subscriber

  Reply # 848464 3-Jul-2013 00:17
Send private message

alasta:
SELECT stuff FROM table1
WHERE id1 IN (SELECT string_id1 FROM table2)
AND id2 IN (SELECT string_id2 FROM table2)


Thanks but no go, syntax errors.

sidefx: Or just use a join: 

http://sqlfiddle.com/#!6/c006c/1


This is what I was originally going for but the select statement is actually a bit more complex containing multiple joined tables already.  I can join my table2 to it but I obviously don't understand the relationships properly and get incorrect data returned.
I guess I was hoping there was some (really inefficient but workable) LOOP I could run.

1173 posts

Uber Geek
+1 received by user: 145

Trusted
Subscriber

  Reply # 848493 3-Jul-2013 07:17
Send private message

Or an older style that may be easier to put into the existing query depending on how it is written.

select t1.stuff from table1 t1, table2 t2
where t1.id1 = t2.id1
and t1.id2 = t2.id2


447 posts

Ultimate Geek
+1 received by user: 104


  Reply # 848507 3-Jul-2013 08:04
Send private message

What about:

SELECT Table1.stuff FROM Table1 INNER JOIN Table2 ON (Table1.id2 = Table2.string_id2) AND (Table1.id1 = Table2.string_id1)

1482 posts

Uber Geek
+1 received by user: 187

Trusted

  Reply # 848516 3-Jul-2013 08:18
Send private message

It really depends what you are trying to achieve.

Alastas SQL query below is fine if you are just trying to find records in table1 which have corresponding id's in table2

SELECT stuff FROM table1
WHERE id1 IN (SELECT string_id1 FROM table2)
AND id2 IN (SELECT string_id2 FROM table2)

However its more efficient to do an "EXISTS" query.

And I'm sorry to say this but if you're having syntax errors on a basic query like this then you probably need to go on a SQL course. Not trying to be mean or anything but Alastas query is basic SQL 101 that you should be able to do. Is there no DBA or someone else at work you can ask for help?

If not try and break the query down into bits that work eg try id1 first like this

SELECT * FROM TABLE1
WHERE ID1 IN (SELECT ID1 FROM TABLE2)

Bear in mind that the IN statement only expects a list of single item values from the SUB-QUERY so you cant do WHERE ID1 IN (SELECT ID1, ID2 FROM TABLE2) or anything like that. Also ID1 in each table should be the same data type otherwise youre going to have more trouble since you need to cast/convert types first.

I would only use a join if the intention was to "join" the two tables and see the information side by side. And as Dolsen said its easier to start with the older join syntax then this new fandangled INNER/OUTER JOIN syntax. Look for Oracle join syntax examples as they mostly show the older syntax.

Trevor

447 posts

Ultimate Geek
+1 received by user: 104


  Reply # 848517 3-Jul-2013 08:30
Send private message

If this is somewhere inside a bigger query, then we probably need to see the entire query to figure it out.

Did you create the database yourself or is it an existing one?

Maybe the structure of the db could be changed to make the query less complex?



620 posts

Ultimate Geek
+1 received by user: 54

Subscriber

  Reply # 848525 3-Jul-2013 08:47
Send private message

tigercorp: ...

sidefx: Or just use a join: 

http://sqlfiddle.com/#!6/c006c/1


This is what I was originally going for but the select statement is actually a bit more complex containing multiple joined tables already.  I can join my table2 to it but I obviously don't understand the relationships properly and get incorrect data returned.


I've received an (unexpected) early morning answer from one of the development team who has provided the correct JOIN conditions to make this work.

I'll mark this as answered, thanks for all the advice everyone!

447 posts

Ultimate Geek
+1 received by user: 104


  Reply # 848526 3-Jul-2013 08:49
2 people support this post
Send private message

tigercorp:
tigercorp: ...

sidefx: Or just use a join: 

http://sqlfiddle.com/#!6/c006c/1


This is what I was originally going for but the select statement is actually a bit more complex containing multiple joined tables already.  I can join my table2 to it but I obviously don't understand the relationships properly and get incorrect data returned.


I've received an (unexpected) early morning answer from one of the development team who has provided the correct JOIN conditions to make this work.

I'll mark this as answered, thanks for all the advice everyone!


Please share, I'm curious now.

Filter this topic showing only the reply marked as answer Create new topic

Twitter »

Follow us to receive Twitter updates when new discussions are posted in our forums:



Follow us to receive Twitter updates when news items and blogs are posted in our frontpage:



Follow us to receive Twitter updates when tech item prices are listed in our price comparison site:





News »

Intel introduces new NUC kits and NUC mini PCs
Posted 16-Aug-2018 11:03


The Warehouse leaps into the AI future with Google
Posted 15-Aug-2018 17:56


Targus set sights on enterprise and consumer growth in New Zealand
Posted 13-Aug-2018 13:47


Huawei to distribute nova 3i in New Zealand
Posted 9-Aug-2018 16:23


Home robot Vector to be available in New Zealand stores
Posted 9-Aug-2018 14:47


Panasonic announces new 2018 OLED TV line up
Posted 7-Aug-2018 16:38


Kordia completes first live 4K TV broadcast
Posted 1-Aug-2018 13:00


Schools get safer and smarter internet with Managed Network Upgrade
Posted 30-Jul-2018 20:01


DNC wants a safer .nz in the coming year
Posted 26-Jul-2018 16:08


Auldhouse becomes an AWS Authorised Training Delivery Partner in New Zealand
Posted 26-Jul-2018 15:55


Rakuten Kobo launches Kobo Clara HD entry level reader
Posted 26-Jul-2018 15:44


Kiwi team reaches semi-finals at the Microsoft Imagine Cup
Posted 26-Jul-2018 15:38


KidsCan App to Help Kiwi Children in Need
Posted 26-Jul-2018 15:32


FUJIFILM announces new high-performance lenses
Posted 24-Jul-2018 14:57


New FUJIFILM XF10 introduces square mode for Instagram sharing
Posted 24-Jul-2018 14:44



Geekzone Live »

Try automatic live updates from Geekzone directly in your browser, without refreshing the page, with Geekzone Live now.



Are you subscribed to our RSS feed? You can download the latest headlines and summaries from our stories directly to your computer or smartphone by using a feed reader.

Alternatively, you can receive a daily email with Geekzone updates.