I have two tables which I am drawing data from
ATM
[ID][Owner][Balance][State]
ATMAvatar
[ATMID][Avatar][Admin][Allowed][Limited][Limit]
The current query I have is
SELECT ATM . * , ATMAvatar.Avatar AS User, ATMAvatar.Admin,
ATMAvatar.Allowed, ATMAvatar.Limited, ATMAvatar.Limit,
(SELECT COUNT( * ) FROM ATMAvatar WHERE ATMID = '{SOME ATM ID}') AS UserCount
FROM ATM
JOIN ATMAvatar ON ATM.ID = ATMAvatar.ATMID
WHERE ATM.ID = '{SOME ATM ID}'
AND ATMAvatar.Avatar = '{SOME NAME}'
Which works well if the ATMAvatar and the ATM both have relative rows. I want to be able to convert this so that the ATM will still be selected with some default values for any of the ATMAvatar columns if the ATMAvatar row can not be found.
For Example
- ID: d7dafb52-bc31-4f38-a433-7c273b345454
- Owner: Tom Hanson
- Balance: 0
- State: 0
- User: James McCrawford (Get this from the query) as the row will not return
- Admin: 0 (Row not available so can't be admin)
- Allowed: 0 (Row not available so can't be allowed)
- Limited: 1 (Row not available so definately limit)
- Limit: 0 (Row not available so limited to $0)
- UserCount: 15
I would be happy if it just returned the rows empty and I can check that in php.
EDIT My problem is I am search for James McCrawford in a selection of rows that does not contain him. I think I need to rethink the structure of the query
Try this one:
SELECT ATM.*,
ATMAvatar.Avatar AS User,
ATMAvatar.Admin,
ATMAvatar.Allowed,
ATMAvatar.Limited,
ATMAvatar.Limit,
( SELECT COUNT(*)
FROM ATMAvatar
WHERE ATMID = '{SOME ATM ID}'
) AS UserCount
FROM ATM
JOIN ATMAvatar ON ATM.ID = ATMAvatar.ATMID
AND ATMAvatar.Avatar = '{SOME NAME}'
WHERE ATM.ID = '{SOME ATM ID}'
0 comments:
Post a Comment