ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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
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.
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?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.