Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 Problem with and if() logic nikos Not Blown Up Yet Posts: 74 Threads: 17 Joined: Jan 2017 Reputation: 0 Likes received: 0 #1 Feb-11-2019, 09:33 PM ``` if name and month and year: cur.execute( '''SELECT * FROM jobs WHERE clientID = (SELECT id FROM clients WHERE name = %s) and MONTH(lastvisit) = %s and YEAR(lastvisit) = %s ORDER BY lastvisit DESC''', (name, month, year) ) print( 1 ) elif name and year: cur.execute( '''SELECT * FROM jobs WHERE clientID = (SELECT id FROM clients WHERE name = %s) and YEAR(lastvisit) = %s ORDER BY lastvisit DESC''', (name, year) ) print( 2 ) elif month and year: cur.execute( '''SELECT * FROM jobs WHERE MONTH(lastvisit) = %s and YEAR(lastvisit) = %s ORDER BY lastvisit DESC''', (month, year) ) print( 3 ) elif year: cur.execute( '''SELECT * FROM jobs WHERE YEAR(lastvisit) = %s ORDER BY lastvisit ASC''', year ) print( 4 ) ```Its almost correct except the fast that the last "elif" never gets executed. Even if 'month=0', which mean no selection of month still this clause 'elif month and year:' always executes instead of the last one. Please can someone correct this and explain why? Thank you! nilamo Last Thursdayist Posts: 3,174 Threads: 80 Joined: Sep 2016 Reputation: 129 Likes received: 707 #2 Feb-11-2019, 09:54 PM ```name = False month = 0 year = 2019 if name and month and year: #cur.execute( '''SELECT * FROM jobs WHERE clientID = (SELECT id FROM clients WHERE name = %s) and MONTH(lastvisit) = %s and YEAR(lastvisit) = %s ORDER BY lastvisit DESC''', (name, month, year) ) print( 1 ) elif name and year: #cur.execute( '''SELECT * FROM jobs WHERE clientID = (SELECT id FROM clients WHERE name = %s) and YEAR(lastvisit) = %s ORDER BY lastvisit DESC''', (name, year) ) print( 2 ) elif month and year: #cur.execute( '''SELECT * FROM jobs WHERE MONTH(lastvisit) = %s and YEAR(lastvisit) = %s ORDER BY lastvisit DESC''', (month, year) ) print( 3 ) elif year: #cur.execute( '''SELECT * FROM jobs WHERE YEAR(lastvisit) = %s ORDER BY lastvisit ASC''', year ) print( 4 )`````````Output:> python eggs.py 4``````I don't get the same output as you, so I'd suggest making sure that your variables actually contain what you think they do. nikos Not Blown Up Yet Posts: 74 Threads: 17 Joined: Jan 2017 Reputation: 0 Likes received: 0 #3 Feb-11-2019, 10:14 PM (This post was last modified: Feb-11-2019, 10:14 PM by nikos. Edited 1 time in total.) ``` if name and month != '0' and year != '0': cur.execute( '''SELECT * FROM jobs WHERE clientID = (SELECT id FROM clients WHERE name = %s) and MONTH(lastvisit) = %s and YEAR(lastvisit) = %s ORDER BY lastvisit DESC''', (name, month, year) ) elif name and year != '0': cur.execute( '''SELECT * FROM jobs WHERE clientID = (SELECT id FROM clients WHERE name = %s) and YEAR(lastvisit) = %s ORDER BY lastvisit DESC''', (name, year) ) elif month != '0' and year != '0': cur.execute( '''SELECT * FROM jobs WHERE MONTH(lastvisit) = %s and YEAR(lastvisit) = %s ORDER BY lastvisit DESC''', (month, year) ) elif year: cur.execute( '''SELECT * FROM jobs WHERE YEAR(lastvisit) = %s ORDER BY lastvisit ASC''', year ) ```Actually when they contained '0', which is a string which is soemthing and its different from 0 which is like sayif if month is not None. « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post need help on logic for even or odd cor41 2 149 Jun-23-2019, 09:36 PM Last Post: Yoriz MCQ logic kferhat 0 120 Jun-16-2019, 08:33 PM Last Post: kferhat first code - need some explaining of logic korenron 11 370 May-12-2019, 09:39 AM Last Post: korenron Logic is not working as it should work Harkishan 1 302 Nov-16-2018, 10:17 AM Last Post: wavic Logic of using floor division and modulus for a different variable at different time SB_J 2 359 Nov-01-2018, 07:25 PM Last Post: SB_J Why is my crawler not recording 404s I think it may be an error in the logic RickyWilson 3 798 Dec-18-2017, 12:57 PM Last Post: Larz60+ Logic error when comparing randomly generated integers SadoDeomeoon 5 1,678 Jun-05-2017, 02:38 PM Last Post: SadoDeomeoon Strange logic! :) alsimm 10 3,415 May-19-2017, 10:10 PM Last Post: volcano63 Number logic problem m1xzg 13 3,861 Oct-23-2016, 09:36 PM Last Post: m1xzg Logic to convert an integer to YEAR / MONTH hugobaur 9 2,593 Oct-18-2016, 11:58 AM Last Post: hugobaur

Forum Jump:

Users browsing this thread: 1 Guest(s)