简体   繁体   English

如何使用插入和选择语句

[英]how to use insert and select statement

we are facing a big problem How can we insert value from 1 column to other column of same table 我们面临一个大问题如何将值从1列插入同一表的其他列

  SELECT *
From (
SELECT  answers.latitude as first_lat,answers.longitude as first_long ,answers.id as first_column  from answers  WHERE answers.question_id=142 and answers.survey_id=194
) as c1,
(
SELECT  answers.latitude as second_lat,answers.longitude as second_long ,answers.id   as second_column from answers  WHERE answers.question_id=146 and answers.survey_id=194
    ) as c2
    where first_column+1=second_column

My above query have following Result But now we are facing problem how can we insert Value from Second_lat to first lat ,long 我上面的查询有以下结果,但现在我们面临的问题是如何将Second_lat的Value插入第一个lat,long

在此处输入图片说明 May it helpfull Actual table structure These are two convective rows in database 146 & 142 Question_id 146 have lat ,long And we want to move lat ,long from 146 to 142 可能对实际的表结构有帮助。这是数据库146和142中的两个对流行。Question_id 146包含lat,long而且我们想将lat,long从146移动到142

  id       question_id ,lat,long
   1     146 ,22.2222.11.1111
   2    142,0,0 
   3     146 ,22.2222.11.1111
   4    142,0,0 

Thanks in advance 提前致谢

UPDATE is the way to do it Something like as below. UPDATE是执行此操作的方法,如下所示。 You might need to run two queries one for longitude and one for latitude. 您可能需要运行两个查询,一个查询经度,一个查询纬度。 UPDATE answers t1 SET t1.longitude = (SELECT t2.longitude FROM answers t2 WHERE t1.rownum + 1 = t2.rownum) 更新答案t1设置t1.longitude =(从答案t2中选择t2.longitude,其中t1.rownum + 1 = t2.rownum)

Depending on the database you are using, that rownum might differ 根据您使用的数据库,该行数可能会有所不同

Give this a try: 试试看:

update answers as a1, answers as a2
  set a1.latitude=a2.latitude,
  a1.longitude=a2.longitude
where a1.id+1=a2.id
  and a1.question_id=142
  and a2.question_id=146
  and a1.survey_id=194
  and a2.survey_id=194

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

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