简体   繁体   English

MySQL查询插入新行

[英]Mysql query insert new row

i have this three tables Retailer, imovo, rbpos_epos. 我有这三个表零售商,imovo,rbpos_epos。 What i do is i match them, and get a result which i print in in a page. 我所做的是我将它们匹配,并得到打印在页面中的结果。

What i need to do is insert those results into a new table, i will make a "Match" button which will query the two tables retailer and imovo, and the matches will go to the new table named :matching. 我需要做的是将这些结果插入到新表中,我将创建一个“匹配”按钮,该查询将查询两个表零售商和imovo,匹配项将进入名为:matching的新表。

What i need in order to avoid duplications.. is to remove from the retailer table and imovo the matched rows.. 为了避免重复,我需要从零售商表中删除并匹配匹配的行。

Here is the query i use to match: 这是我用来匹配的查询:

 $data = mysql_query( "select r.`time`, r.`epos_id`, r.`date`,re.`location`,i.`user_id`,i.`mobile_number`,
       i.`time`, i.`rbpos_id`, i.`date`
from retailer r
join rbpos_epos re on r.epos_id = re.epos_id
join imovo i on i.rbpos_id = re.rbpos_id
             and addtime(r.`time`, '0:0:50') > i.`time`
             and r.`time` < i.`time`
             and r.`date` = i.`date`;
") 
 or die(mysql_error()); 
 Print "<table border cellpadding=3>"; 
  Print "<tr>"; 
 Print "<th>Date:</th>
<th>Time</th>
<th>Location</th>
<th>User ID</th> 
<th>Mobile Number</th>"; 
  Print "</tr>";
 while($info = mysql_fetch_array( $data )) 
 { 

   Print "<tr>"; 
 Print " <td>".$info['date'] . "</td>
<td>".$info['time'] . " </td>
<td>".$info['location'] . " </td>
<td>".$info['user_id'] . " </td>
<td>".$info['mobile_number'] . " </td>
</tr>"; 
 } 

The printed rows are those i need to insert in the matching table.. Thanks! 打印的行是我需要在匹配表中插入的行。谢谢!

I tried this: 我尝试了这个:

      $data = mysql_query( "INSERT INTO Matching(date, time, location, user_id, phone)
SELECT 
   r.`date`,
   `time`,
   re.`location`,
   i.`user_id`,
   i.`mobile_number`
from retailer r
join rbpos_epos re on r.epos_id = re.epos_id
join imovo i on i.rbpos_id = re.rbpos_id
and addtime(r.`time`, '0:0:50') > i.`time`
and r.`time` < i.`time`
and 

r. date = i. date =我。 date ; date ; ") But i get the error: “)但我得到了错误:

Column count doesn't match value count at row 1

Use INSERT INTO ... SELECT ... : 使用INSERT INTO ... SELECT ...

INSERT INTO Matching(date, time, location, user_id, phone)
SELECT 
   r.`date`,
   `time`,
   re.`location`,
   i.`user_id`,
   i.`mobile_number`
from retailer r
join rbpos_epos re on r.epos_id = re.epos_id
join imovo i on i.rbpos_id = re.rbpos_id
and addtime(r.`time`, '0:0:50') > i.`time`
and r.`time` < i.`time`
and r.`date` = i.`date`;

Then you have to do this INSERT when the button match is clicked. 然后,当单击按钮match时,必须执行此INSERT

Update: If you want to delete the matched rows. 更新:如果要删除匹配的行。 You can DELETE with JOIN . 您可以使用JOIN DELETE Something like: 就像是:

DELETE r, re, i
from retailer r
join rbpos_epos re on r.epos_id = re.epos_id
join imovo i on i.rbpos_id = re.rbpos_id
and DATE(r.`date`) = DATE(i.`date`);

This will delete all the matched rows from all of the three tables, as explained in the manual: 如手册中所述,这将从所有三个表中删除所有匹配的行:

For the multiple-table syntax, DELETE deletes from each tbl_name the rows that satisfy the conditions. 对于多表语法,DELETE从每个tbl_name中删除满足条件的行。

SQL Fiddle Demo SQL小提琴演示

Note that: it is better to use single column DATETIME instead of two separate parts date and time like what I did in the demo. 注意:最好使用单列DATETIME而不是像演示中那样使用两个单独的datetime部分。 Then you can use the MySQL Date and time functions to control them, like what I did in the join condition DATE(r.date) = DATE(i.date) . 然后,您可以使用MySQL日期和时间函数来控制它们,就像我在DATE(r.date) = DATE(i.date)条件DATE(r.date) = DATE(i.date)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM