有人能帮我获得已填充MySQL列的计数吗?
例如:我的表格结构:
ID | FName | LName |
---|---|---|
1 | ABC | NULL |
2 | XYZ |
所以,在搜索了一两个小时之后。
我使用这个作为我的解决方案,但我不确定重新分级优化是否好。这是我正在使用的:
function ranking_mechanism( $currentUserID ){
global $wpdb;
$rank = 0;
$results = $wpdb->get_row(
$wpdb->prepare('SELECT * FROM TABLE WHERE ID=%d', $currentUserID)
);
$finalArr = (array) $results; // Convert $results as Array(to perform iteration), it(WordPress DB Query) typically return Object by Default
foreach ( $finalArr as $key => $Table ) {
if( $finalArr[$key] == '' || $finalArr[$key] == 'NULL' ){
// Nothing to do
}else{
$rank = $rank+1; // Increase Rank If Found Value
}
return $rank;
}
SELECT ID, (ID IS NOT NULL) /* assuming that ID is numeric and cannot be empty string */
+ (FName <> '' AND FName IS NOT NULL)
+ (LName <> '' AND LName IS NOT NULL) AS `Count`
FROM source_table
-- WHERE ID = ???