简体   繁体   中英

Mysql query from three tables based on date

I'm trying to get user information based on their subscription startdate in MYSQL.

This is what i've come up with this far..

    SELECT a.id, a.user_login, a.user_email, c.cardtype, c.accountnumber, a.user_pass, a.user_registered
    FROM users a
    INNER JOIN memberships_users b
    ON b.user_id = a.id
    INNER JOIN memberships_order c
    ON c.user_id = a.id
    WHERE b.status = 'active' AND 
          (c.cyckle_period = 6 AND 
          ('2016-06-01 00:00:00' < (SELECT c.startdate ORDER BY c.startdate DESC LIMIT 1)) 
          OR 
          (c.cyckle_period = 1 AND 
          ('2015-12-01 00:00:00' < (SELECT c.startdate ORDER BY c.startdate DESC LIMIT 1));

This query gives me error message "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIMIT 0, 30' at line 8".

I don't see any LIMIT 0, 30 in your posted code. did you post the right code? BTW, your this below part is totally wrong

'2015-12-01 00:00:00' < (SELECT c.startdate ORDER BY c.startdate DESC LIMIT 1)

It should rather be

'2015-12-01 00:00:00' < SELECT max(startdate) FROM memberships_order

Change your WHERE part like below

WHERE b.status = 'active' AND 
      c.cyckle_period IN (6, 1) AND 
      '2015-12-01 00:00:00' < (SELECT max(startdate) FROM memberships_order);

I think you are just missing right parenthesis. According to me it should be,

SELECT a.id, a.user_login, a.user_email, c.cardtype, c.accountnumber, a.user_pass, a.user_registered FROM users a INNER JOIN memberships_users b ON b.user_id = a.id INNER JOIN memberships_order c ON c.user_id = a.id WHERE b.status = 'active' AND (c.cyckle_period = 6 AND ('2016-06-01 00:00:00' < (SELECT c.startdate ORDER BY c.startdate DESC LIMIT 1))) OR (c.cyckle_period = 1 AND ('2015-12-01 00:00:00' < (SELECT c.startdate ORDER BY c.startdate DESC LIMIT 1)));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM