Need MySQL Query help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Falcon Capt
    Senior Member
    • May 2006
    • 237
    • 4.2.x

    Need MySQL Query help

    I am trying to find users who have had one subscription but not the other.

    I am using this query to locate one of the subscription types:

    SELECT username
    FROM subscriptionlog
    LEFT JOIN user ON (user.userid = subscriptionlog.userid)
    WHERE subscriptionid = 6 AND status = 0 AND usergroupid = 2

    Now out of this group I need to find ONLY the users who have NOT had subscriptionid = 1.

    Basically, if there userid exists in subscriptionlog more than once (indicating more than one subscription has existed for the user) I don't want the username returned. ONLY users who have had subscriptionid 6 and NOT 1.

    Any help would be greatly appreciated!
  • ThorstenA
    Senior Member
    • Nov 2004
    • 3082
    • 4.0.x

    #2
    For sql queries, you'll find good answers in this forum:



    This forum is about vBulletin 3.8 Questions, Problems and Troubleshooting on a not so technical basis.

    Comment

    • Falcon Capt
      Senior Member
      • May 2006
      • 237
      • 4.2.x

      #3
      Originally posted by ThorstenA
      For sql queries, you'll find good answers in this forum:



      This forum is about vBulletin 3.8 Questions, Problems and Troubleshooting on a not so technical basis.
      Thanks. I have seen answers to MySQL queries here and was hoping to possibly receive some helpful info. I have also posted the same question at VB.org.

      Comment

      • Edwin Brown
        Former vBulletin Developer
        • Mar 2009
        • 1393
        • 5.5.x

        #4
        Originally posted by Falcon Capt
        I am trying to find users who have had one subscription but not the other.

        I am using this query to locate one of the subscription types:

        SELECT username
        FROM subscriptionlog
        LEFT JOIN user ON (user.userid = subscriptionlog.userid)
        WHERE subscriptionid = 6 AND status = 0 AND usergroupid = 2

        Now out of this group I need to find ONLY the users who have NOT had subscriptionid = 1.

        Basically, if there userid exists in subscriptionlog more than once (indicating more than one subscription has existed for the user) I don't want the username returned. ONLY users who have had subscriptionid 6 and NOT 1.

        Any help would be greatly appreciated!
        This is complex enough that a bit of testing is required, but I would so another self-join. The first join can be inner, and the second a left. I'm not sure if the "status" applied to both joins, but I think so.


        SELECT username
        FROM subscriptionlog
        inner JOIN user AS user1 ON (user.userid = subscriptionlog.userid and subscriptionid = 6 AND user1.usergroupid = 2 and status = 0)
        LEFT JOIN user AS user2 ON (user.userid = subscriptionlog.userid subscriptionid = 1 AND user2.usergroupid = 2 and status = 0)
        WHERE user2.userid is null;
        Please- I'm not tech support. Don't send your problem reports to me unless I've asked you to.

        Comment

        • ThorstenA
          Senior Member
          • Nov 2004
          • 3082
          • 4.0.x

          #5
          Wow, that is an amazing first post! Thanks

          Comment

          • Falcon Capt
            Senior Member
            • May 2006
            • 237
            • 4.2.x

            #6
            Originally posted by Edwin Brown

            SELECT username
            FROM subscriptionlog
            inner JOIN user AS user1 ON (user.userid = subscriptionlog.userid and subscriptionid = 6 AND user1.usergroupid = 2 and status = 0)
            LEFT JOIN user AS user2 ON (user.userid = subscriptionlog.userid subscriptionid = 1 AND user2.usergroupid = 2 and status = 0)
            WHERE user2.userid is null;
            Will this still require the use of "WHERE" conditional? I noticed you didn't include it.

            Should it possibly be:

            SELECT username
            FROM subscriptionlog
            inner JOIN user AS user1 ON (user.userid = subscriptionlog.userid WHERE subscriptionid = 6 AND user1.usergroupid = 2 and status = 0)
            LEFT JOIN user AS user2 ON (user.userid = subscriptionlog.userid WHERE subscriptionid = 1 AND user2.usergroupid = 2 and status = 0)
            WHERE user2.userid is null;

            I will test it when I get home later today. Any further inputis greatly apprciated!

            Comment

            • Kevin Sours
              Lead Developer
              • Apr 2008
              • 601
              • 5.5.x

              #7
              Originally posted by Falcon Capt
              Will this still require the use of "WHERE" conditional? I noticed you didn't include it.

              Should it possibly be:

              SELECT username
              FROM subscriptionlog
              inner JOIN user AS user1 ON (user.userid = subscriptionlog.userid WHERE subscriptionid = 6 AND user1.usergroupid = 2 and status = 0)
              LEFT JOIN user AS user2 ON (user.userid = subscriptionlog.userid WHERE subscriptionid = 1 AND user2.usergroupid = 2 and status = 0)
              WHERE user2.userid is null;

              I will test it when I get home later today. Any further inputis greatly apprciated!
              WHERE isn't used in JOIN clauses at all (that's not a valid query). ON essentially takes the place of WHERE in the join.
              Kevin

              Comment

              • Falcon Capt
                Senior Member
                • May 2006
                • 237
                • 4.2.x

                #8
                Originally posted by Edwin Brown
                This is complex enough that a bit of testing is required, but I would so another self-join. The first join can be inner, and the second a left. I'm not sure if the "status" applied to both joins, but I think so.


                SELECT username
                FROM subscriptionlog
                inner JOIN user AS user1 ON (user.userid = subscriptionlog.userid and subscriptionid = 6 AND user1.usergroupid = 2 and status = 0)
                LEFT JOIN user AS user2 ON (user.userid = subscriptionlog.userid subscriptionid = 1 AND user2.usergroupid = 2 and status = 0)
                WHERE user2.userid is null;
                OK, got a couple errors...

                When using this query:

                SELECT user
                FROM subscriptionlog
                inner JOIN user AS user1 ON (user.userid = subscriptionlog.userid AND subscriptionid = 6 AND user1.usergroupid = 2 AND status = 0)
                LEFT JOIN user AS user2 ON (user.userid = subscriptionlog.userid AND subscriptionid = 1 AND user2.usergroupid = 2 AND status = 0)
                WHERE user2.userid is null;
                I got this error:

                An error occurred while attempting to execute your query. The following information was returned.
                error number: 1054
                error desc: Unknown column 'user' in 'field list'


                When I changed the query to this:

                SELECT *
                FROM subscriptionlog
                inner JOIN user AS user1 ON (user.userid = subscriptionlog.userid AND subscriptionid = 6 AND user1.usergroupid = 2 AND status = 0)
                LEFT JOIN user AS user2 ON (user.userid = subscriptionlog.userid AND subscriptionid = 1 AND user2.usergroupid = 2 AND status = 0)
                WHERE user2.userid is null;
                I got this error:

                An error occurred while attempting to execute your query. The following information was returned.
                error number: 1054
                error desc: Unknown column 'user.userid' in 'on clause'
                Any help would be greatly appreciated!

                Comment

                • Falcon Capt
                  Senior Member
                  • May 2006
                  • 237
                  • 4.2.x

                  #9
                  Anyone have any updates/other ideas?

                  Comment

                  • Kevin Sours
                    Lead Developer
                    • Apr 2008
                    • 601
                    • 5.5.x

                    #10
                    You've aliased both user tables so there is no table "user" in the query... just user1 and user2.
                    Kevin

                    Comment

                    • Falcon Capt
                      Senior Member
                      • May 2006
                      • 237
                      • 4.2.x

                      #11
                      Ok, I am getting closer...

                      Using this query:

                      Code:
                      SELECT user1.username
                      FROM subscriptionlog
                      inner JOIN user AS user1 ON (user1.userid = subscriptionlog.userid AND subscriptionid = 6 AND status = 0 AND user1.usergroupid = 2)
                      LEFT JOIN user AS user2 ON (user2.userid = subscriptionlog.userid AND subscriptionid = 1 AND status = 0 AND user2.usergroupid = 2)
                      WHERE user2.userid is null;
                      I don't get any errors BUT it isn't returning the proper data. I want ONLY members who have had a subscriptionid=6 that have NOT had a subscriptionid=1 (active or expired). The above query is returning only everyone who has had a subscriptionid=6 that is inactive (regardless if they have had a subscriptionid=1 or not).

                      I am guessing something minor needs to be adjusted to get the proper data.

                      Any help is greatly appreciated!

                      Comment

                      • Kevin Sours
                        Lead Developer
                        • Apr 2008
                        • 601
                        • 5.5.x

                        #12
                        I think its backwards

                        try
                        Code:
                        SELECT user.userid
                        FROM user JOIN 
                          subscriptionlog sl1 ON 
                         (user.userid = sl.userid AND sl.subscriptionid = 6 AND sl.status = 0 AND user.usergroupid = 2) LEFT JOIN
                          subscriptionlog sl2 ON 
                         (user.userid = sl2.userid AND sl2.subscriptionid = 1 AND sl.status = 0 AND user.usergroupid = 2)
                        WHERE sl2.userid IS NULL

                        Comment

                        • Falcon Capt
                          Senior Member
                          • May 2006
                          • 237
                          • 4.2.x

                          #13
                          Originally posted by Kevin Sours
                          I think its backwards

                          try
                          Code:
                          SELECT user.userid
                          FROM user JOIN 
                            subscriptionlog sl1 ON 
                           (user.userid = sl.userid AND sl.subscriptionid = 6 AND sl.status = 0 AND user.usergroupid = 2) LEFT JOIN
                            subscriptionlog sl2 ON 
                           (user.userid = sl2.userid AND sl2.subscriptionid = 1 AND sl.status = 0 AND user.usergroupid = 2)
                          WHERE sl2.userid IS NULL
                          I get the following error:

                          An error occurred while attempting to execute your query. The following information was returned.
                          error number: 1054
                          error desc: Unknown column 'sl.userid' in 'on clause'

                          Comment

                          • Falcon Capt
                            Senior Member
                            • May 2006
                            • 237
                            • 4.2.x

                            #14
                            Ok, I THINK I've got it!!!

                            The above needed a little modification, but it appears to work now using the following query!

                            Code:
                            [FONT=Courier New]SELECT user.username[/FONT]
                            [FONT=Courier New]FROM user JOIN [/FONT]
                            [FONT=Courier New]subscriptionlog sl1 ON [/FONT]
                            [FONT=Courier New](user.userid = sl1.userid AND sl1.subscriptionid = 6 AND sl1.status = 0 AND user.usergroupid = 2) LEFT JOIN[/FONT]
                            [FONT=Courier New]subscriptionlog sl2 ON [/FONT]
                            [FONT=Courier New](user.userid = sl2.userid AND sl2.subscriptionid = 1 AND sl2.status = 0 AND user.usergroupid = 2)[/FONT]
                            [FONT=Courier New]WHERE sl2.userid IS NULL[/FONT]
                            Thanks to everyone who offered up their help!
                            Last edited by Falcon Capt; Fri 10 Jul '09, 7:50pm.

                            Comment

                            • Falcon Capt
                              Senior Member
                              • May 2006
                              • 237
                              • 4.2.x

                              #15
                              Ok guys, need a tad more help... The above query did what I needed and returned everyone who had one subscription (subscriptionid = 6) but not the other (subscriptionid = 1).

                              Now I need a query of everyone who is in Usergroupid = 2 but has not had either of the above subscriptions. (basically their userid isn't included in the subscriptionlog at all)...

                              I tried the following but it didn't work...

                              Code:
                              SELECT user.username
                              FROM user
                              JOIN subscriptionlog sl1 ON 
                              (user.userid = sl1.userid AND sl1.subscriptionid = 6 AND sl1.status = 0 AND user.usergroupid = 2)
                              WHERE sl1.userid IS NULL 
                              LEFT JOIN subscriptionlog sl2 ON 
                              (user.userid = sl2.userid AND sl2.subscriptionid = 1 AND sl2.status = 0)
                              WHERE sl2.userid IS NULL

                              Comment

                              widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
                              Working...