Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pymysql ifnull
#1
I am using IFNULL in a select statement with a group by & rollup . In the mysql work bench it works perfectly but when I use it in python-pymysql in stead of replacing the null with my literal it repeats the last value from the last row.

The code is :-

sql = ('SELECT IFNULL(categories.desc,"Total") as Category, YEAR(trans_dat), sum(transactions.amount) '
           'FROM transactions '
           'inner join vendor on transactions.acct_no = vendor.acct_no '
           'inner join categories on vendor.cat_1 = categories.cat_no '
           'group by cat_1 with rollup ')
With mysql I get :-

Car
Comms
Entertainment
Food
Health
House Hold
Misc
Payments
Personal
School
Transport
Travel
Total

With python - pymysql I get :-

[['Food', '-31163.28', 0, 0, 0], ['Car', '-29888.81', 0, 0, 0], ['Entertainment', '-10250.68', 0, 0, 0], ['Health', '-22569.32', 0, 0, 0], ['Transport', '-4065.60', 0, 0, 0], ['Comms', '-4367.56', 0, 0, 0], ['School', '-3073.55', 0, 0, 0], ['House Hold', '-29560.66', 0, 0, 0], ['Travel', '-976.04', 0, 0, 0], ['Personal', '-152201.22', 0, 0, 0], ['Payments', '251313.75', 0, 0, 0], ['Misc', '-2319.53', 0, 0, 0], ['Misc', '-39122.50', 0, 0, 0]]
Reply
#2
can you show your full code. Cursor would return tuples, not lists. That is why I guess the output is what you print, i.e. it may be problem with your output, not what is returned from the database.
Also, you may want to try using the offical mysql connector - https://dev.mysql.com/doc/connector-pyth...eface.html
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
This is the section of code (Note: this is only my second attempt at writing a program):-

su_stat = []
indx = 0

sql = ('SELECT IFNULL(categories.desc,"Total") as Category, YEAR(trans_dat), sum(transactions.amount) '

       'FROM transactions '
       'inner join vendor on transactions.acct_no = vendor.acct_no '
       'inner join categories on vendor.cat_1 = categories.cat_no '
       'group by cat_1 with rollup ')

cursor.execute(sql)
su_row = cursor.fetchall()
print(su_row)
for row in su_row:
    print(row)
    su_stat.append(row)


for row in su_stat:
    print(row)
    my_c1 = row[0]
    my_amt = str(row[2])

    sudat[indx][0] = my_c1
    sudat[indx][1] = my_amt

    indx = indx + 1
    print(sudat)
    print(indx)

connection.close()
The individual rows are:-

Output:
('Food', 2018, Decimal('-31163.28')) ('Car', 2018, Decimal('-29888.81')) ('Entertainment', 2018, Decimal('-10250.68')) ('Health', 2018, Decimal('-22569.32')) ('Transport', 2018, Decimal('-4065.60')) ('Comms', 2018, Decimal('-4367.56')) ('School', 2017, Decimal('-3073.55')) ('House Hold', 2016, Decimal('-29560.66')) ('Travel', 2018, Decimal('-976.04')) ('Personal', 2018, Decimal('-152201.22')) ('Payments', 2016, Decimal('251313.75')) ('Misc', 2018, Decimal('-2319.53')) ('Misc', 2018, Decimal('-39122.50'))
Reply
#4
Found the answer - I should have group by categories.desc - this fixed the problem
Reply
#5
(Sep-06-2018, 01:01 AM)aland Wrote: I should have group by categories.desc - this fixed the problem
That is strange as you said original one works with mysql ad workbench...
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Mysql Syntax error in pymysql ilknurg 4 2,343 May-18-2022, 06:50 AM
Last Post: ibreeden
  pymysql can't do SELECT * Pedroski55 3 2,953 Sep-11-2021, 10:18 PM
Last Post: Pedroski55
  pymysql won't handle some diacritic characters awarren2001AD 0 1,261 Apr-16-2020, 08:58 AM
Last Post: awarren2001AD
  pyMySQL - ROW_NUMBER in SQL statement JayCee 1 2,373 Apr-12-2020, 08:40 PM
Last Post: JayCee
  pyMySQL How do I get the row id JayCee 3 2,702 Apr-12-2020, 08:38 PM
Last Post: JayCee
  PyMySQL return a single dictionary Valon1981 2 1,770 Feb-20-2020, 04:07 PM
Last Post: Valon1981
  pymysql: formating ouput of query wardancer84 18 8,322 Oct-04-2018, 01:54 PM
Last Post: wardancer84
  get last row id in pymysql tony1812 1 11,151 Sep-26-2017, 11:25 PM
Last Post: Larz60+
  pymysql question tony1812 1 2,882 Sep-26-2017, 12:41 AM
Last Post: Larz60+
  pymysql on macos seirra tony1812 5 4,538 Sep-17-2017, 03:24 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020