SQL statement help

Hello experts.

I have a table storing user updates and another table storing icons for those updates. At the moment it works where it gets all updates for a user based on User id. I am now wanting to join another table "aspnet_Membership" and to say.

only show user updates where aspnet_Membership.CreateDate > tbl_latestupdates.DateAdded

records from the aspnet_Membership table are retrieved from a variable called strUserId so I'd imagine it would be WHERE aspnet_Membership = " + strUserId

Can anyone help?

Thanks


sb.Append("SELECT TOP 8 ");
        sb.Append("tbl_latestupdates.LULinkText_" + up.UserLanguage + " LULinkText, ");
        sb.Append("tbl_latestupdates.LULinkURL, ");
        sb.Append("tbl_latestupdates.DateAdded, ");
        sb.Append("tbl_latestupdates.LUUserId, ");
        sb.Append("tbl_latestupdatesicons.LUIconPath ");
        sb.Append("FROM tbl_latestupdates ");
        sb.Append("INNER JOIN tbl_latestupdatesicons ");
        sb.Append("ON tbl_latestupdates.LUImageId = tbl_latestupdatesicons.LUIconId ");
        sb.Append("WHERE (tbl_latestupdates.LUUserId = @UserProfileId) ");
        sb.Append("OR (tbl_latestupdates.LUUserId = - 1) ");
        sb.Append("ORDER BY tbl_latestupdates.DateAdded DESC");

Open in new window

thomasmuttonAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SujithData ArchitectCommented:
Assuming that strUserId returns a single row from the aspNetMembership table.
	sb.Append("SELECT TOP 8 ");
        sb.Append("tbl_latestupdates.LULinkText_" + up.UserLanguage + " LULinkText, ");
        sb.Append("tbl_latestupdates.LULinkURL, ");
        sb.Append("tbl_latestupdates.DateAdded, ");
        sb.Append("tbl_latestupdates.LUUserId, ");
        sb.Append("tbl_latestupdatesicons.LUIconPath ");
        sb.Append("FROM tbl_latestupdates ");
        sb.Append("INNER JOIN tbl_latestupdatesicons ");
        sb.Append("ON tbl_latestupdates.LUImageId = tbl_latestupdatesicons.LUIconId ");
	sb.Append("CROSS JOIN aspnet_Membership ");
        sb.Append("WHERE (tbl_latestupdates.LUUserId = @UserProfileId) ");
        sb.Append("OR (tbl_latestupdates.LUUserId = - 1) ");
        sb.Append("AND aspnet_Membership = " + strUserId + " ");
        sb.Append("ORDER BY tbl_latestupdates.DateAdded DESC");
 

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
thomasmuttonAuthor Commented:
Hi, I tried this but it came up with this error.

Incorrect syntax near 'fa'.

on

GridView1.DataBind();
sb.Append("SELECT TOP 8 ");
        sb.Append("tbl_latestupdates.LULinkText_" + up.UserLanguage + " LULinkText, ");
        sb.Append("tbl_latestupdates.LULinkURL, ");
        sb.Append("tbl_latestupdates.DateAdded, ");
        sb.Append("tbl_latestupdates.LUUserId, ");
        sb.Append("tbl_latestupdatesicons.LUIconPath ");
        sb.Append("FROM tbl_latestupdates ");
        sb.Append("INNER JOIN tbl_latestupdatesicons ");
        sb.Append("ON tbl_latestupdates.LUImageId = tbl_latestupdatesicons.LUIconId ");
        sb.Append("CROSS JOIN aspnet_Membership ");
        sb.Append("WHERE (tbl_latestupdates.LUUserId = @UserProfileId) ");
        sb.Append("OR (tbl_latestupdates.LUUserId = - 1) ");
        sb.Append("AND aspnet_Membership.UserId = " + strUserId + " ");
        sb.Append("ORDER BY tbl_latestupdates.DateAdded DESC");

Open in new window

0
thomasmuttonAuthor Commented:
and yes, every record in the aspnet_Membership table has its own unique UserId
0
The Five Tenets of the Most Secure Backup

Data loss can hit a business in any number of ways. In reality, companies should expect to lose data at some point. The challenge is having a plan to recover from such an event.

aflockhartCommented:
If you are comparing string values you need to enclose them in single quotes.

e.g.  (notice the extra quote marks before and after strUserId) ...

sb.Append("AND aspnet_Membership.UserId = '" + strUserId + "' ");
0
SujithData ArchitectCommented:
yes the single quotes should do.
0
thomasmuttonAuthor Commented:
Hi guys, putting the single quotes worked fine. I am still having problems with this tho.

Basically if we wanted to have the update sent to everyone, we would put the latest update User Id as -1

I think there is something wrong with the logic as it says WHERE the user id = 13 OR latest update UserId = -1 AND Membership User Id = whatever

should the latest update User Id be before the cross join?
StringBuilder sb = new StringBuilder();
        sb.Append("SELECT TOP 8 ");
        sb.Append("tbl_latestupdates.LULinkText_" + up.UserLanguage + " LULinkText, ");
        sb.Append("tbl_latestupdates.LULinkURL, ");
        sb.Append("tbl_latestupdates.DateAdded, ");
        sb.Append("tbl_latestupdates.LUUserId, ");
        sb.Append("tbl_latestupdatesicons.LUIconPath ");
        sb.Append("FROM tbl_latestupdates ");
        sb.Append("INNER JOIN tbl_latestupdatesicons ");
        sb.Append("ON tbl_latestupdates.LUImageId = tbl_latestupdatesicons.LUIconId ");
        sb.Append("CROSS JOIN aspnet_Membership ");
        sb.Append("WHERE (tbl_latestupdates.LUUserId = @UserProfileId) ");
        sb.Append("OR (tbl_latestupdates.LUUserId = -1) ");
        sb.Append("AND aspnet_Membership.UserId = '" + strUserId + "' ");
        sb.Append("AND tbl_latestupdates.DateAdded > aspnet_Membership.CreateDate ");
        sb.Append("ORDER BY tbl_latestupdates.DateAdded DESC");

Open in new window

0
thomasmuttonAuthor Commented:
I know what is wrong with this but i dont know how to solve it.

if tbl_latestupdates.LUUserId = -1 then the aspnet_Membership record will not exist.
sb.Append("WHERE (tbl_latestupdates.LUUserId = @UserProfileId) ");
        sb.Append("OR (tbl_latestupdates.LUUserId = -1) ");
        sb.Append("AND aspnet_Membership.UserId = '" + strUserId + "' ");
        sb.Append("AND tbl_latestupdates.DateAdded > aspnet_Membership.CreateDate ");

Open in new window

0
aflockhartCommented:
Depends why you need to link to the aspnet_Membership  table in the first place, and how you want to handle the situation where there is no membership record.

I would look at bracketing your conditions, so you only check the records from aspnet_Membership  when the value of tbl_latestupdates.LUUserId  is not -1  .



0
aflockhartCommented:
e.g.

WHERE ( (userID =@userprofileID) and (membership.userid=@strUnserID) and (dateadded > membership.createdate )  )
OR
(userid=-1)

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.