好的,我有以下内容:
user id meta
2 _qty 3
2 Weight 20
1 _qty 1
1 weight 30
我需要获取用户 ID 列,然后将数量乘以权重,得到用户 ID 2= 60 和用户 ID = 1 30…… 我尝试了以下但无济于事:
$myrows = $wpdb->get_results( "SELECT meta_value, SUM(_qty*weight) AS product_id FROM {$wpdb->prefix}woocommerce_order_itemmeta GROUP BY ($query_select_order_items)" );
其中 ($query_select_order_items) 是 user_id。
我该怎么做?
我当前正在运行的实际 php:
/**
* Returns all the orders made by the user
*
* @param int $user_id
* @param string $status (completed|processing|canceled|on-hold etc)
* @return array of order ids
*/
function fused_get_all_user_orders($user_id,$status='completed'){
if(!$user_id)
return false;
$orders=array();//order ids
$args = array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => $user_id,
'post_type' => 'shop_order',
'post_status' => 'publish',
/* 'tax_query'=>array(
array(
'taxonomy' =>'shop_order_status',
'field' => 'slug',
'terms' =>$status
)
) */
);
$posts=get_posts($args);
//get the post ids as order ids
$orders=wp_list_pluck( $posts, 'ID' );
return $orders;
}
function fused_get_all_products_ordered_by_user($user_id=false){
$orders=fused_get_all_user_orders($user_id);
if(empty($orders))
return false;
$order_list='('.join(',', $orders).')';//let us make a list for query
//so we have all the orders made by this user which was successfull
//we need to find the products in these order and make sure they are downloadable
// find all products in these order
global $wpdb;
$query_select_order_items="SELECT order_item_id as id FROM {$wpdb->prefix}woocommerce_order_items WHERE order_id IN {$order_list}";
$query_select_product_ids="SELECT meta_value as product_id FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE meta_key=%s AND order_item_id IN ($query_select_order_items)";
$products=$wpdb->get_col($wpdb->prepare($query_select_product_ids,'weight'));
$qty = $wpdb->get_col($wpdb->prepare($query_select_product_ids,'_qty'));
return $products;
}
目前 $products 最后返回一个重量列表,或者如果我将它设置为 _qty 它将返回一个订购数量列表... sqlfiddle
草莓回答更新:
"SELECT order_item_id, weight * quantity total FROM (
SELECT order_item_id,
MAX(CASE WHEN meta_key = '_qty' THEN meta_value ELSE 0 END) quantity ,
MAX(CASE WHEN meta_key = 'weight' THEN meta_value ELSE 0 END) weight
FROM wp_woocommerce_order_itemmeta GROUP BY order_item_id)
WHERE order_item_id IN ($query_select_order_items) x;"
最佳答案
这是一种方法...
DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table
(entity INT NOT NULL
,attribute VARCHAR(12) NOT NULL
,value INT NOT NULL
,PRIMARY KEY(entity,attribute)
);
INSERT INTO my_table VALUES
(2,'quantity',3),
(2,'weight',20),
(1,'quantity',1),
(1,'weight',30);
SELECT entity, weight * quantity total
FROM
( SELECT entity
, MAX(CASE WHEN attribute = 'quantity' THEN value END) quantity
, MAX(CASE WHEN attribute = 'weight' THEN value END) weight
FROM my_table
GROUP
BY entity
) x;
+--------+-------+
| entity | total |
+--------+-------+
| 1 | 30 |
| 2 | 60 |
+--------+-------+
...甚至只是...
SELECT entity
, ROUND(EXP(SUM(LOG(value)))) total
FROM my_table
GROUP
BY entity;
+--------+-------+
| entity | total |
+--------+-------+
| 1 | 30 |
| 2 | 60 |
+--------+-------+
使第一个查询适应您的 fiddle 可能看起来像这样......
SELECT order_item_id, weight * quantity total
FROM
( SELECT order_item_id
, MAX(CASE WHEN meta_key = '_qty' THEN meta_value ELSE 0 END) quantity
, MAX(CASE WHEN meta_key = 'weight' THEN meta_value ELSE 0 END) weight
FROM wp_woocommerce_order_itemmeta
GROUP
BY order_item_id
) x;
...第二个可能看起来像这样...
SELECT order_item_id
, ROUND(EXP(SUM(LOG(meta_value)))) total
FROM wp_woocommerce_order_itemmeta
WHERE meta_key IN('_qty','weight')
GROUP
BY order_item_id;
... 并根据您的最新要求调整第一个...
SELECT i.order_item_id, weight * quantity total
FROM
( SELECT order_item_id
, MAX(CASE WHEN meta_key = '_qty' THEN meta_value ELSE 0 END) quantity
, MAX(CASE WHEN meta_key = 'weight' THEN meta_value ELSE 0 END) weight
FROM wp_woocommerce_order_itemmeta
GROUP
BY order_item_id
) x
JOIN wp_woocommerce_order_items i
ON i.order_item_id = x.order_item_id
WHERE i.order_id IN(647,649,650);
关于php - 在 MySql 中获取两列并将结果相乘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24058100/
有没有办法在这个简单的get方法中添加超时选项?我正在使用法拉第3.3。Faraday.get(url)四处寻找,我只能先发起连接后应用超时选项,然后应用超时选项。或者有什么简单的方法?这就是我现在正在做的:conn=Faraday.newresponse=conn.getdo|req|req.urlurlreq.options.timeout=2#2secondsend 最佳答案 试试这个:conn=Faraday.newdo|conn|conn.options.timeout=20endresponse=conn.get(url
我有一个存储主机名的Ruby数组server_names。如果我打印出来,它看起来像这样:["hostname.abc.com","hostname2.abc.com","hostname3.abc.com"]相当标准。我想要做的是获取这些服务器的IP(可能将它们存储在另一个变量中)。看起来IPSocket类可以做到这一点,但我不确定如何使用IPSocket类遍历它。如果它只是尝试像这样打印出IP:server_names.eachdo|name|IPSocket::getaddress(name)pnameend它提示我没有提供服务器名称。这是语法问题还是我没有正确使用类?输出:ge
我想获取模块中定义的所有常量的值:moduleLettersA='apple'.freezeB='boy'.freezeendconstants给了我常量的名字:Letters.constants(false)#=>[:A,:B]如何获取它们的值的数组,即["apple","boy"]? 最佳答案 为了做到这一点,请使用mapLetters.constants(false).map&Letters.method(:const_get)这将返回["a","b"]第二种方式:Letters.constants(false).map{|c
我安装了ruby版本管理器,并将RVM安装的ruby实现设置为默认值,这样'哪个ruby'显示'~/.rvm/ruby-1.8.6-p383/bin/ruby'但是当我在emacs中打开inf-ruby缓冲区时,它使用安装在/usr/bin中的ruby。有没有办法让emacs像shell一样尊重ruby的路径?谢谢! 最佳答案 我创建了一个emacs扩展来将rvm集成到emacs中。如果您有兴趣,可以在这里获取:http://github.com/senny/rvm.el
假设我有这个范围:("aaaaa".."zzzzz")如何在不事先/每次生成整个项目的情况下从范围中获取第N个项目? 最佳答案 一种快速简便的方法:("aaaaa".."zzzzz").first(42).last#==>"aaabp"如果出于某种原因你不得不一遍又一遍地这样做,或者如果你需要避免为前N个元素构建中间数组,你可以这样写:moduleEnumerabledefskip(n)returnto_enum:skip,nunlessblock_given?each_with_indexdo|item,index|yieldit
我目前正在使用以下方法获取页面的源代码:Net::HTTP.get(URI.parse(page.url))我还想获取HTTP状态,而无需发出第二个请求。有没有办法用另一种方法做到这一点?我一直在查看文档,但似乎找不到我要找的东西。 最佳答案 在我看来,除非您需要一些真正的低级访问或控制,否则最好使用Ruby的内置Open::URI模块:require'open-uri'io=open('http://www.example.org/')#=>#body=io.read[0,50]#=>"["200","OK"]io.base_ur
如何在Ruby中获取BasicObject实例的类名?例如,假设我有这个:classMyObjectSystem我怎样才能使这段代码成功?编辑:我发现Object的实例方法class被定义为returnrb_class_real(CLASS_OF(obj));。有什么方法可以从Ruby中使用它? 最佳答案 我花了一些时间研究irb并想出了这个:classBasicObjectdefclassklass=class这将为任何从BasicObject继承的对象提供一个#class您可以调用的方法。编辑评论中要求的进一步解释:假设你有对象
是否可以在应用程序中包含的gem代码中知道应用程序的Rails文件系统根目录?这是gem来源的示例:moduleMyGemdefself.included(base)putsRails.root#returnnilendendActionController::Base.send:include,MyGem谢谢,抱歉我的英语不好 最佳答案 我发现解决类似问题的解决方案是使用railtie初始化程序包含我的模块。所以,在你的/lib/mygem/railtie.rbmoduleMyGemclassRailtie使用此代码,您的模块将在
我有一个应用程序可以读取文件的内容并为其编制索引。我将它们存储在磁盘本身中,但现在我使用的是AmazonS3,因此以下方法不再适用。事情是这样的:defperform(docId)@document=Document.find(docId)if@document.file?#Youshould'tcreateanewversion@document.versionlessdo|doc|@document.file_content=Cloudoc::Extractor.new.extract(@document.file.file)@document.saveendendend@docu
导读语言模型给我们的生产生活带来了极大便利,但同时不少人也利用他们从事作弊工作。如何规避这些难辨真伪的文字所产生的负面影响也成为一大难题。在3月9日智源Live第33期活动「DetectGPT:判断文本是否为机器生成的工具」中,主讲人Eric为我们讲解了DetectGPT工作背后的思路——一种基于概率曲率检测的用于检测模型生成文本的工具,它可以帮助我们更好地分辨文章的来源和可信度,对保护信息真实、防止欺诈等方面具有重要意义。本次报告主要围绕其功能,实现和效果等展开。(文末点击“阅读原文”,查看活动回放。)Ericmitchell斯坦福大学计算机系四年级博士生,由ChelseaFinn和Chri