简体   繁体   English

用特殊的“关键字”(SQL)计算表行

[英]Count table rows with special “keyword” (SQL)

I have a SQL-table called "usermeta" , inside this table are lots of columns and rows. 我有一个名为“ usermeta”SQL表 ,该表内有许多列和行。 I need the column "meta_key" and want to count the rows with the keyword "Users" . 我需要“ meta_key”列,并想用关键字“ Users”对行进行计数

Whats wrong with my script? 我的脚本怎么了?

<?php
$user_count = $wpdb->get_var( "SELECT COUNT(meta_key) AS Users FROM $wpdb->usermeta" );
echo "<p>Users count: {$user_count}</p>";
?>

SQL query should be SQL查询应该是

SELECT COUNT(DISTINCT meta_key) FROM  usermeta WHERE meta_key LIKE '%Users%'

and since we did not want unique results 由于我们不希望获得独特的结果

I always get "1" - does it only counts this entry once? 我总是得到“ 1”-它只计算一次该条目吗? – Pepe –佩佩

and code should be therefore 因此代码应该是

<?php
$user_count = $wpdb->get_var( "SELECT COUNT(meta_key) AS Users FROM $wpdb->usermeta WHERE meta_key like '%Users%'" );
echo "<p>Users count: {$user_count}</p>";
?>

You should probably use a WHERE clause to filter for rows with have your keyword. 您可能应该使用WHERE子句过滤具有关键词的行。 For a detailed answer you should explain more one how your table actually looks like. 有关详细的答案,您应该解释一下表格的实际外观。

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

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