使用php计算字段总数的查询

时间:2015-05-18 05:03:47

标签: mysql sql database

我正在使用PHP。我想写一个满足某些条件的查询。

CREATE TABLE IF NOT EXISTS `wp_frm_item_metas` (
  `id` int(11) NOT NULL,
  `meta_value` longtext,
  `field_id` int(11) NOT NULL,
  `item_id` int(11) NOT NULL,
  `created_at` datetime NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=200 DEFAULT CHARSET=utf8;

表格结构:

INSERT INTO `wp_frm_item_metas` (`id`, `meta_value`, `field_id`, `item_id`, `created_at`) VALUES
(2072, '591.00', 198, 181, '2015-05-17 22:10:02'),
(1640, 'Complete', 253, 156, '2015-05-14 21:38:26'),
(1714, 'Complete', 253, 161, '2015-05-16 02:11:50'),
(1747, 'Complete', 253, 163, '2015-05-16 02:23:13'),
(1909, 'Complete', 253, 172, '2015-05-17 01:12:11'),
(2094, 'Yes', 278, 182, '2015-05-17 23:31:03'),
(2113, 'Yes', 278, 183, '2015-05-17 23:42:11'),
(2090, '2015-05-19', 212, 182, '2015-05-17 23:31:03'),
(2109, '2015-05-20', 212, 183, '2015-05-17 23:42:11'),
(2100, '106', 270, 182, '2015-05-17 23:31:03'),
(2119, '106', 270, 183, '2015-05-17 23:42:11');

SELECT sum(meta_value) 
  FROM wp_frm_item_metas 
  WHERE field_id=198 
    and meta_value=
     (select meta_value 
       from wp_frm_item_metas 
       where field_id=253 
         and meta_value="Complete") 
    and meta_value=
      (select meta_value 
        from wp_frm_item_metas 
        where field_id=278  
          and meta_value="Yes") 
    and meta_value=
      (select meta_value 
        from wp_frm_item_metas 
        where field_id=270 
          and meta_value=106). 

表格数据

set @sSql =  'SELECT [Datetime] as Busidate,storenum,TransactionID,TransType, Cardnumber,Amount FROM [dts_SBTransInfo]inner join storeno st on st.number = dts_sbtransinfo.storenum WHERE  ErrorCode <> ''9009'' and isnull(void,0) = 0 
and  [Datetime] between   convert(varchar(10),''' + @sStartDate + ''', 112) and  convert(varchar(10),''' + @sEndDate + ''', 112)'  

我尝试了此查询,但它显示错误为:

  

子查询返回超过1行

如何在元数据中找到总和&#39;满足上述条件的领域?

2 个答案:

答案 0 :(得分:0)

您可以在子查询中使用btplpinoudaynpddpsugjevklbvh/Build/Products/Debug- iphonesimulator/MyBook.app/Frameworks/RealmSwift.framework/strip- frameworks.sh: No such file or directory ,这样就不会出错。

答案 1 :(得分:0)

这样可以避免错误,也可能是你真正想要的(尽管我看不到那些field_id和meta_values之间的关系......)

SELECT sum(meta_value) 
  FROM wp_frm_item_metas 
  WHERE field_id=198 
    and EXISTS
     (select meta_value 
       from wp_frm_item_metas 
       where field_id=253 
         and meta_value="Complete") 
    and EXISTS
      (select meta_value 
        from wp_frm_item_metas 
        where field_id=278  
          and meta_value="Yes") 
    and EXISTS
      (select meta_value 
        from wp_frm_item_metas 
        where field_id=270 
          and meta_value=106)