English 中文(简体)
使用选择和分组的方式丢弃缺失表达式来更新语句
原标题:Update statement using select and group by throwing missing expression

我试图用来自另一个表格的数据更新一个表格列 deletiondate , 但我得到了“ 缺失表达式” 错误。 有人能帮助我解决这个问题吗?

基本上我要在表格中更新 < code> delectate 列, 将关键字段与另一表格合并, 并分组。 如果日期为 < code> 01- JAN-0001 , 记录计数为 & gt; 1, 那么需要更新 < code> 01- JAN-0001 , 则我需要更新最大删除日期值 。

更新报表一使用了:

update table1 db1 set deletiondate =

SELECT  
CASE WHEN count(*)>1 and ( 
    (select 1 
     from table2 b 
     where b.loginid = a.loginid
         and a.creationdate = b.creationdate 
         and b.deletiondate =  01-JAN-0001 
     ) = 1) THEN  01-JAN-0001  ELSE to_char(MAX(deletiondate), DD-MON-YYYY ) END as deletiondate1  
FROM table2 a 
GROUP BY a.loginid, a.creationdate 
WHERE db1.userloginid = a.loginid and db1.usercreationdate = a.creationdate 
问题回答

使用此格式 : < a href=" "http://www.sqlfiddle.com/#.4/c46a6/2" rel="nofollow" >http://www.sqlfiddle.com/#.4/c46a6/2

update product set
  (total_qty,max_qty) = 
(
  select sum(qty) as tot, max(qty) as maxq
  from inv
  where product_id = product.product_id
  group by product_id
) ;

抽样数据:

create table product(
  product_id int primary key,
  product_name varchar(100),
  total_qty int,
  max_qty int
);

create table inv(
  inv_id int primary key,
  product_id int references product(product_id),
  qty int
);


insert into product(product_id,product_name) 
select 1, KB  from dual
union
select 2,  MSE  from dual
union
select 3,  CPU  from dual;

insert into inv(inv_id,product_id,qty)
select 1,1,4 from dual
union
select 2,2,1 from dual
union
select 3,3, 3 from dual
union
select 4,1,1 from dual
union
select 5,2,2 from dual
union
select 6,1,5 from dual;

查询输出 :

| PRODUCT_ID | PRODUCT_NAME | TOTAL_QTY | MAX_QTY |
|------------|--------------|-----------|---------|
|          1 |           KB |        10 |       5 |
|          2 |          MSE |         3 |       2 |
|          3 |          CPU |         3 |       3 |




相关问题
what is wrong with this mysql code

$db_user="root"; $db_host="localhost"; $db_password="root"; $db_name = "fayer"; $conn = mysqli_connect($db_host,$db_user,$db_password,$db_name) or die ("couldn t connect to server"); // perform query ...

Users asking for denormalized database

I am in the early stages of developing a database-driven system and the largest part of the system revolves around an inheritance type of relationship. There is a parent entity with about 10 columns ...

Easiest way to deal with sample data in Java web apps?

I m writing a Java web app in my free time to learn more about development. I m using the Stripes framework and eventually intend to use hibernate and MySQL For the moment, whilst creating the pages ...

join across databases with nhibernate

I am trying to join two tables that reside in two different databases. Every time, I try to join I get the following error: An association from the table xxx refers to an unmapped class. If the ...

How can I know if such value exists in database? (ADO.NET)

For example, I have a table, and there is a column named Tags . I want to know if value programming exists in this column. How can I do this in ADO.NET? I did this: OleDbCommand cmd = new ...

Convert date to string upon saving a doctrine record

I m trying to migrate one of my PHP projects to Doctrine. I ve never used it before so there are a few things I don t understand. In my current code, I have a class similar to this: class ...

热门标签