MSFN Forum: Simple SQL query question - MSFN Forum

Jump to content


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Simple SQL query question Sorry, I couldn't find where to post an SQL question. Rate Topic: -----

#1 User is offline   ciHnoN 

  • Member
  • PipPip
  • Group: Banned
  • Posts: 153
  • Joined: 22-January 13
  • OS:none specified
  • Country: Country Flag

Posted 29 January 2013 - 07:29 PM

Sorry, I wasn't sure where to post an SQL specific question. Please move this thread to the correct place. :)

I think I'm getting stupid or something, not sure why I cannot solve this! *lol*

I got two tables TABLE1 and TABLE2. TABLE1 has some item ids which can have one or more linked values from TABLE2.

Example:

TABLE1.ID
TABLE1.ITEMID
TABLE1.table2_ID


TABLE2.ID
TABLE2.DESCRIPTION


Data sample:

TABLE1:

ID ---- ITEMID ---- table2_ID
1 ---- SOMEITEMa ---- 2
2 ---- SOMEITEMa ---- 3
3 ---- SOMEITEMa ---- 4
4 ---- SOMEITEMb ---- 2
5 ---- SOMEITEMc ---- 2


TABLE2:
ID ---- DESCRIPTION
1 ---- somedescription001
2 ---- somedescription002
3 ---- somedescription003
4 ---- somedescriptoin004

As you see, in TABLE1, I got 3 items "SOMEITEMa","SOMEITEMb","SOMEITEMc" which all share "description002" connected by TABLE2.ID to them.


My question is: How do I select from TABLE1 only those ITEMIDs which have TABLE2.ID='2' and NO OTHER IDs connected to them? I basically want the result to show two rows from TABLE1, which is "SOMEITEMb" and "SOMEITEMc", and not "SOMEITEMa" because that item has other values connected to it. Eventhough is has TABLE2.ID='2' connected to it, I don't want to select it because it has other IDs from TABLE2 as well.

Thanks! :)




#2 User is offline   Tripredacus 

  • K-Mart-ian Legend
  • Group: Super Moderator
  • Posts: 8,665
  • Joined: 28-April 06
  • OS:Server 2012
  • Country: Country Flag

Posted 30 January 2013 - 10:36 AM

Which SQL are you using? MSSQL, MySQL or something else?

Should be close enough for web development. When in doubt, if there is no section we always have the Hardware and Software sections.

#3 User is offline   ciHnoN 

  • Member
  • PipPip
  • Group: Banned
  • Posts: 153
  • Joined: 22-January 13
  • OS:none specified
  • Country: Country Flag

Posted 30 January 2013 - 01:28 PM

I'm using MSSQL. (Thanks)

#4 User is offline   submix8c 

  • Inconceivable!
  • Group: Patrons
  • Posts: 3,241
  • Joined: 14-September 05
  • OS:none specified
  • Country: Country Flag

Posted 30 January 2013 - 02:31 PM

Been a LOOOONG time using SQL (MainFrame via Assembly/COBOL) but it seems that somewhere you will have to allow for a "count" or another "secondary" Read to see if there's another "ITEMID" existing (thus indicating more than one Table2_ID and/or building the Report in an independent temporary Table?

How are the Key Fields set up - just the ID field in both tables? On a Mainframe, you could use a Secondary Key as necessary (depending on your needs).

(that's an oddball you're asking for)

Probably no help, but logically that seems to be what you may need to do.

#5 User is offline   ciHnoN 

  • Member
  • PipPip
  • Group: Banned
  • Posts: 153
  • Joined: 22-January 13
  • OS:none specified
  • Country: Country Flag

Posted 30 January 2013 - 02:46 PM

Thanks for the help submix8c but I really need an example of a SQL query. Kind'a urgent, need it for something at my work. hehe

#6 User is offline   submix8c 

  • Inconceivable!
  • Group: Patrons
  • Posts: 3,241
  • Joined: 14-September 05
  • OS:none specified
  • Country: Country Flag

Posted 30 January 2013 - 02:52 PM

Sorry I can't help beyond "logically". Only have used basic functions in MS-Access so far (very similar). Now if this were being coded in said Mainframe languages (and access to said Mainframe), I could have give you exactly what you need post-haste. Have no full-blown SQL (MS-Access isn't AFAICT) installed. :(

"For Each..."/ "End-For"

#7 User is offline   Tripredacus 

  • K-Mart-ian Legend
  • Group: Super Moderator
  • Posts: 8,665
  • Joined: 28-April 06
  • OS:Server 2012
  • Country: Country Flag

Posted 30 January 2013 - 03:54 PM

Sounds like you need to do a JOIN. Read about the types here (generally speaking)
http://www.w3schools...ql/sql_join.asp

Remember MSSQL and MySQL has slightly different syntax.

#8 User is offline   ciHnoN 

  • Member
  • PipPip
  • Group: Banned
  • Posts: 153
  • Joined: 22-January 13
  • OS:none specified
  • Country: Country Flag

Posted 30 January 2013 - 05:02 PM

Thanks for the link. I know about JOIN. I been coding a long while now. But this specific scenario doesn't cover it. A bit tricky. :e :huh:

#9 User is offline   ciHnoN 

  • Member
  • PipPip
  • Group: Banned
  • Posts: 153
  • Joined: 22-January 13
  • OS:none specified
  • Country: Country Flag

Posted 30 January 2013 - 06:54 PM

*solved it* :)

Turns out I had the IDs from second table so I only used one table.

Something like:

SELECT id FROM table

LEFT JOIN

(SELECT id FROM table WHERE <some condition>[mine was id<10 or id>20]) AS A

ON table.id=A.id

WHERE A.id IS NULL <---- the important part. <---- the important part.

Cheers!




Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



All trademarks mentioned on this page are the property of their respective owners
Copyright © 2001 - 2013 msfn.org
Privacy Policy