How to count non empty rows from 'naziv_operacije'? You can see table on the picture
If the rows are empty strings or null, the below should do it:
SELECT COUNT(*)
FROM `TEHN`
WHERE `naziv_operacije` IS NOT NULL OR `naziv_operacije` != '';
Try this:
select count(column_name) from naziv_operacije;
Replace column_name with the column which should not be empty
The documentation of COUNT() explains:
COUNT(expr)
Returns a count of the number of non-NULLvalues ofexprin the rows retrieved by aSELECTstatement. The result is aBIGINT` value.
COUNT(*) is somewhat different in that it returns a count of the number of rows retrieved, whether or not they contain NULL values.
If by "empty" you mean NULL then COUNT(naziv_operacije) is all you need.
Run this query to see how it works:
SELECT
COUNT(naziv_operacije) AS not_empty,
COUNT(*) AS all
FROM TEHN
Related
I have a query where I am trying to find a matching tag from a comma separated value in a db Table, the value in the database may look like this:
"Tags":"all, kids, family, cqr, october"
and my query right now looks like this:
$rows = $db -> select("
SELECT `FriendlyId`,`Name`,`Description`,`Tags`,`BeginDate`,`EndDate`
FROM `Pages`
WHERE `SiteId` = '569e435701395'
AND `IsActive` = '1'
AND `BeginDate` IS NOT NULL
AND FIND_IN_SET('kids', Tags)
ORDER BY `BeginDate` ASC");
Unfortunately, this is returning an empty result even though I know there are rows in the db that match the query. This is my first time ever using FIND_IN_SET so am I doing something wrong?
I have table with 10 columns and I want to check input value in where clause of the MySQL query.
I want to do something like this. But, when I use this query I am getting an error.
for example :
SELECT * FROM user_data
where poll_title='$poll_title'
and '$voter' IN (user_vote_1,user_vote_2,user_vote_3...user_vote_10)
order by idpoll ASC
user_vote_1 to 10 (value is null'ed in the database) and I want to retrieve only that rows from a column which have $voter value.
I think you need this comparison (Not Sure OfCourse) :-
SELECT * FROM user_data
where poll_title = "$poll_title"
and (user_vote_1 = "$voter"
OR user_vote_2 = "$voter"
OR user_vote_3 = "$voter"
OR user_vote_4 = "$voter"......OR user_vote_10 = "$voter")
order by idpoll ASC
If I've understood what you want to do - return only the column with the value - then would coalesce do the job? This assumes that the value in user_vote_n will either match the value you're looking for or be null, since coalesce returns the first non-null argument.
(untested)
select coalesce(user_vote_1, user_vote_2, user_vote_3, ) as UserVote from user_data
where coalesce(user_vote_1, user_vote_2, user_vote_3, ) = '$voter';
That aside, this looks like a structure that could do with normalising - a single 'user_vote' column and a single 'user_vote_number' column.
I have this query:
$butacas= $this->pdo->prepare('SELECT COUNT( * ) FROM `usuarios` WHERE `sala` LIKE :nombreSala');
$butacas->bindValue(':nombreSala', $nombreSala);
$butacas->execute();
echo $butacas->rowCount();
This query results in an integer: 1
if I replace :nombreSala with the value that actually exists in the database, like:
SELECT COUNT( * ) FROM usuarios WHERE sala LIKE 'salaChica'
It still results in 1.
Now, when checking the database with phpMyadmin I realise that with that value (salaChica) there are 2 items instead of one! (and consulting from phpMyadmin does result in 2).
Why it is not accurate? I've read this post about that function not being always accurate with SELECT, but is there a simple alternative?
You're confusing the number of rows returned (1) with the value returned (2).
Your query "SELECT count(*) WHERE whatever" will always return 1 row (unless you do a group by). that row contains a single column - the value of that column will be the count - 2 in your case.
Try
echo $butacas->fetchColumn();
instead of
echo $butacas->rowCount();
I have the following query in PHP:
$titlematch = mysql_query("SELECT `item_id` FROM `items` WHERE `item_id` IN ($matches_s) AND MATCH (`eng_name`) AGAINST ('$query') IN NATURAL LANGUAGE MODE AS score;");
$titlematch2 = mysql_fetch_assoc($titlematch);
In plain words, I want to select the item_id from the table items where
the item_id value can be found within the array $matches_s, and
the string stored in eng_name can be found within the string $query
and then output a 2-dimensional array, where the second-level array contains the item_id and score (relevance) elements.
I have tried switching the order of which criteria comes first, or using brackets. Here are the results I get:
var_dump($titlematch); // returns bool(false)
var_dump($titlematch2); // returns NULL
Question is 'What am I doing wrong? Do I have to write this differently to other AND statements?
I have a table user with column data which has most of the fields empty so it is basically NULL. I want to count the no: rows with data present in that column.
ONE WAY : So I thought I could count the no: NULL fields and subtracts from the total row.
ANOTHER WAY: I could directly count the occupied fields( but which i apparently don't know). So I wrote this for the 1st method....but the count it gives is 0. It is not able identify the NULL or whatever.
$sql = "SELECT COUNT(CASE WHEN data = '' THEN 1 END) AS null_count, FROM users";
$real= mysql_query($sql);
while($row = mysql_fetch_assoc($real)) {
echo "null Count: {$row['null_count']}". '<br />';
}
SELECT SUM(IF(data IS NULL, 1, 0)) AS null_count FROM users
You have to use IS NULL since NULL is not equal to an empty string. It isn't even equal to NULL!
SELECT COUNT(CASE WHEN data IS_NULL THEN 1 END) AS null_count
To find non-empty do it this way:
SELECT
COUNT(CASE WHEN len(data) > 0 THEN 1 else 0 END) AS not_empty_or_null_count,
FROM
users
If you don't want to count items with all spaces you can call trunc() on data before len()
Similar question (for SQL Server)
Find fields that aren't used (have all nulls)
any reason why
select count(field) from table where field is not null;
won't suffice?