Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query

Status
Not open for further replies.

Bell1991

Programmer
Joined
Aug 20, 2003
Messages
386
Location
US
I have two tables:

TableTransaction
NameFrom -> references TableName(NameID)
NameTo -> references TableName(NameID)
ActionID -> references TableAction (ActionID)

TableAction
ActionID
ActionName

TableName
NameID
Name

I need to get all columns in TableTransaction where
NameFrom = Name and ActionID = 1 or 2 or 3 or 4
and all columns from TableTransaction where
NameTo = Name and ActionID = 5


 
think a better explanation of your table structure is needed - confusing the life out of me anyhow :-)

[bandito] [blue]DBomrrsm[/blue] [bandito]

[blue]Software code, like laws and sausages, should never be examined in production[/blue][black] - [/black][purple]Edward Tenner[/purple]
 
You can't link both nameFrom and nameTo at the same time otherwise you end up with a circular reference which will probably fail. try this:

select t.NameFrom, t.NameTo, t.ActionID
from TableTransaction t, TableName n
where
(t.NameFrom = n.NameID
and
(t.ActionID = 1
or t.ActionID = 2
or t.ActionID = 3
or t.ActionID = 4)
and n.Name = "expected name")
and
(t.NameTo = n.NameID
and
n.Name = "expected name"
and
t.ActionID = 5)


you don't need TableAction as the id is already in TableTransaction. I would suggest trying the where clause separately to make sure it is right, i'm a little rusty! Once you can get them separately, encase them in brackets and put an 'and' between them
 
I think these should give you what you want:

Code:
SELECT t.*
FROM tabletransaction t JOIN tablename n ON t.namefrom = n.nameid
WHERE n.name = 'joe bloggs'
  AND t.actionid BETWEEN 1 AND 4

SELECT t.*
FROM tabletransaction t JOIN tablename n ON t.nameto = n.nameid
WHERE n.name = 'joe bloggs'
  AND t.actionid = 5

--James
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top