LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-23-2021, 12:16 PM   #1
rahulvishwakarma
Member
 
Registered: Aug 2010
Posts: 138

Rep: Reputation: 2
sql query problem in c++ and mysql


hi to all, i've centos 7.5 in VM. I've a problem with sql query. here i want to display product, customer and billing information by a one sql. here is a program.

Code:
void classBill::todaySales()
{
    mysql = classConn::connection();
    mysql->reconnect = true;

    draw->clrscr();
    draw->drawRect();

    draw->gotoxy(15, 3);

    cout << "Sales By Date";
    draw->gotoxy(13,4);
    cout << "-----------------";
    draw->gotoxy(10, 5);
    cout << "Enter Date to see sale : ( yyyy / MM/ dd ) : ";
    do
    {
        flag = true;
        draw->gotoxy(55, 5);
        ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
        for(int i = 55; i < w.ws_col -1; i++)
        {
            cout  << " ";
        }
        draw->gotoxy(55, 5);
        getline(cin,strdate);
        if(strdate.empty())
        {
            flag == true;
        }
        else
        {
            flag = classValid::isValidDate(strdate);
            if(!flag)
            {
                draw->gotoxy(10, 6);
                cout << "invalid date please re-enter";
                strdate.clear();
                getc->getch();
                draw->gotoxy(10, 6);
                ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);

                for(int i = 10; i < w.ws_col - 1; i++)
                {
                    cout << " ";
                }
            }
        }
    }while(flag == false);


    if(strdate.empty() == true)
    {
        date *dt = new date;
        time_t now = time(0);
        tm *ltm = std::localtime(&now);
        dt->yy = ltm->tm_year;
        dt->mm = ltm->tm_mon+1;
        dt->dd = ltm->tm_mday;

        strdate = std::to_string(1900 + dt->yy) + "/" + std::to_string(dt->mm) + "/" + std::to_string(dt->dd);
        draw->gotoxy(10, 6);
        cout << "date : " << strdate << endl;
    }
    else
    {
        draw->gotoxy(10, 6);
        cout << "date : " << strdate << endl;
    }

    mysql = classConn::connection();
    // here is sql query
    sql = "select p.productname, p.rate, b.quantity, b.total, b.billno, c.customername, c.contactaddress, c.mobileno, c.id as CustomerID from tableProductRecords as p, tableBilling as ";
    sql += "b, tableCustomers as c where b.dateofsale = '"+ strdate+"' and p.productname in ( select productname from tableProductRecords where productid = ";
    sql += "b.productid ) and c.customername in (select customername from tableCustomers where billno = b.billno) order by b.billno;"; 

    qstate = mysql_query(mysql, sql.c_str());
    if(!qstate)
    {
        res = mysql_store_result(mysql);
        lines = sqlp->process_result_set(mysql, res, 10, totallen);
    }
    else
    {
        draw->gotoxy(10, 7);
        cout << "Error in todays sale : " << mysql_error(mysql);
        getc->getch();
        return;
    }

    sql = "select sum(total) from  tableBilling  where dateofsale = '"+strdate+"';";
    mysql = classConn::connection();
    qstate = mysql_query(mysql, sql.c_str());
    if(!qstate)
    {
        res = mysql_store_result(mysql);
        if((row = mysql_fetch_row(res)) != nullptr)
        {
            if(row[0] != nullptr)
            {
                gtotal = std::stoi(row[0]);
            }
            else
            {
                gtotal = 0;
            }
        }
    }
    else
    {
        draw->gotoxy(10, lines + 25);
        cout << "Error in sum(total) : " << mysql_error(mysql);
        getc->getch();
    }

    draw->gotoxy(10, lines + 15);
    cout << "Grand total : " << gtotal;

    getc->getch();
}
now problem is that sql query returns unwanted rows. as in these pics. here cutomer id "2" is not included in billno = 18, customer id 2 is having bill no 2 not 18 butit customerid 2 is showing in billno with 18. how to get only rows that have correct billno and customer id.
Attached Thumbnails
Click image for larger version

Name:	tableBilling.png
Views:	15
Size:	228.4 KB
ID:	36672   Click image for larger version

Name:	tablecustomers.png
Views:	6
Size:	240.5 KB
ID:	36673   Click image for larger version

Name:	tableProductRecords.png
Views:	8
Size:	233.7 KB
ID:	36674  
 
Old 06-23-2021, 01:51 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
It would be helpful if you could include the table structure and separate out the query instead of including all the code, which is mostly irrelevant to the question.

But on a first look your query really makes little sense as you appear to be selecting records by customer name from a subquery which selects those names from the same table by billing number, and similarly for product names which come from a subquery of the same table by product id. And you are producing the full Cartesian product of those tables so every customer will appear with every billing number - your query produces every combination not just those which are actually related.

First, get rid of the subqueries as they appear to be unneeded and try to reduce your query to just the necessary bits. And use the keys for record selection, not the names. And join on those keys instead of taking the Cartesian product of those records.

Last edited by astrogeek; 06-23-2021 at 01:53 PM. Reason: potys
 
Old 06-23-2021, 04:08 PM   #3
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by rahulvishwakarma View Post
hi to all, i've centos 7.5 in VM. I've a problem with sql query. here i want to display product, customer and billing information by a one sql. here is a program.
Code:
void classBill::todaySales(){
    mysql = classConn::connection();
    mysql->reconnect = true;

    draw->clrscr();
    draw->drawRect();

    draw->gotoxy(15, 3);

    cout << "Sales By Date";
    draw->gotoxy(13,4);
    cout << "-----------------";
    draw->gotoxy(10, 5);
    cout << "Enter Date to see sale : ( yyyy / MM/ dd ) : ";
    do
    {
        flag = true;
        draw->gotoxy(55, 5);
        ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
        for(int i = 55; i < w.ws_col -1; i++)
        {
            cout  << " ";
        }
        draw->gotoxy(55, 5);
        getline(cin,strdate);
        if(strdate.empty())
        {
            flag == true;
        }
        else
        {
            flag = classValid::isValidDate(strdate);
            if(!flag)
            {
                draw->gotoxy(10, 6);
                cout << "invalid date please re-enter";
                strdate.clear();
                getc->getch();
                draw->gotoxy(10, 6);
                ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);

                for(int i = 10; i < w.ws_col - 1; i++)
                {
                    cout << " ";
                }
            }
        }
    }while(flag == false);


    if(strdate.empty() == true)
    {
        date *dt = new date;
        time_t now = time(0);
        tm *ltm = std::localtime(&now);
        dt->yy = ltm->tm_year;
        dt->mm = ltm->tm_mon+1;
        dt->dd = ltm->tm_mday;

        strdate = std::to_string(1900 + dt->yy) + "/" + std::to_string(dt->mm) + "/" + std::to_string(dt->dd);
        draw->gotoxy(10, 6);
        cout << "date : " << strdate << endl;
    }
    else
    {
        draw->gotoxy(10, 6);
        cout << "date : " << strdate << endl;
    }

    mysql = classConn::connection();
    // here is sql query
    sql = "select p.productname, p.rate, b.quantity, b.total, b.billno, c.customername, c.contactaddress, c.mobileno, c.id as CustomerID from tableProductRecords as p, tableBilling as ";
    sql += "b, tableCustomers as c where b.dateofsale = '"+ strdate+"' and p.productname in ( select productname from tableProductRecords where productid = ";
    sql += "b.productid ) and c.customername in (select customername from tableCustomers where billno = b.billno) order by b.billno;"; 

    qstate = mysql_query(mysql, sql.c_str());
    if(!qstate)
    {
        res = mysql_store_result(mysql);
        lines = sqlp->process_result_set(mysql, res, 10, totallen);
    }
    else
    {
        draw->gotoxy(10, 7);
        cout << "Error in todays sale : " << mysql_error(mysql);
        getc->getch();
        return;
    }

    sql = "select sum(total) from  tableBilling  where dateofsale = '"+strdate+"';";
    mysql = classConn::connection();
    qstate = mysql_query(mysql, sql.c_str());
    if(!qstate)
    {
        res = mysql_store_result(mysql);
        if((row = mysql_fetch_row(res)) != nullptr)
        {
            if(row[0] != nullptr)
            {
                gtotal = std::stoi(row[0]);
            }
            else
            {
                gtotal = 0;
            }
        }
    }
    else
    {
        draw->gotoxy(10, lines + 25);
        cout << "Error in sum(total) : " << mysql_error(mysql);
        getc->getch();
    }

    draw->gotoxy(10, lines + 15);
    cout << "Grand total : " << gtotal;

    getc->getch();
}
now problem is that sql query returns unwanted rows. as in these pics. here cutomer id "2" is not included in billno = 18, customer id 2 is having bill no 2 not 18 butit customerid 2 is showing in billno with 18. how to get only rows that have correct billno and customer id.
At this point, you've been working with C++ for ten years, and have asked many questions about C++ and MySQL:
https://www.linuxquestions.org/quest...es-4175686406/
https://www.linuxquestions.org/quest...-c-4175686608/
https://www.linuxquestions.org/quest...ql-4175690481/
https://www.linuxquestions.org/quest...27-4175673488/
https://www.linuxquestions.org/quest...-a-4175689917/
https://www.linuxquestions.org/quest...es-4175686406/

You rarely (if ever) follow up on your threads, and as a programmer with a decade of experience, you should be able to debug your own code. Have you put any effort forward into doing debugging yet?
 
  


Reply

Tags
c++, mysql



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Moving with SQL Server to Linux? Move from SQL Server to MySQL as well! LXer Syndicated Linux News 0 11-28-2016 02:02 PM
FreeRadius: Failed to open file '/etc/freeradius/sql/mysql/schema.sql', error: 13 firewallengineer Linux - Networking 1 07-20-2016 02:43 PM
[SOLVED] Converting SQL Scripts for MS SQL Server to Postgresql or MySQL markush Programming 4 09-12-2013 02:09 PM
[SOLVED] MySQL: Comparing values in diff tables SQL Query and BASH watice Programming 4 12-24-2012 12:56 PM
Problem with SQL query using MySQL gigglesnorter Programming 2 04-22-2004 04:11 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:25 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration