SQLite合并两个表

Qt ComboBox : SQLite Merge Two Table

本文关键字:两个 合并 SQLite      更新时间:2023-10-16

我需要一些帮助。这是我的普通组合框的代码,它将通过选择客户ID来显示客户信息。如何从其他表(LOAN)显示数据& &;还可以在组合框上按客户id从CUSTOMER表中获取信息。

void LoanRequest::on_comboBox_custID_activated(const QString &arg1)
{    
    Login conn;
    if (!conn.connOpen())
    {
      qDebug()<<"Failed to open the database.";
      return;
    }
   conn.connOpen();
   QSqlQuery qry;
   if(qry.exec("SELECT * FROM CUSTOMER WHERE Cust_ID='"+arg1+"'"))        
   {
      //database table, my 2 table that i want to merge
      //LOAN : loanid, loan_type, custid,loan_status
      //CUSTOMER : custid, custname, custic,custaddress, custtelno
      while(qry.next())
      {
        ui->label_name->setText(qry.value(2).toString());
        ui->label_icno->setText(qry.value(3).toString());
        ui->label_telno->setText(qry.value(5).toString());
      }
  }
  else
  {
     QMessageBox::critical(this,tr("Error"),qry.lastError().text());
  }      
  conn.connClose();
}

试试这样:

if(qry.exec("SELECT CUSTOMER.custname, 
            CUSTOMER.custic,CUSTOMER.custtelno,LOAN.loanid  
            FROM CUSTOMER JOIN LOAN WHERE CUSTOMER.custid = LOAN.custid 
            AND Cust_ID='"+arg1+"'"))        
{
  //database table, my 2 table that i want to merge
  //LOAN : loanid, loan_type, custid,loan_status
  //CUSTOMER : custid, custname, custic,custaddress, custtelno
  while(qry.next())
  {
    ui->label_name->setText(qry.value(2).toString());
    ui->label_icno->setText(qry.value(3).toString());
    ui->label_telno->setText(qry.value(5).toString());
  }
}