I have a database table with only 1 column. Each row/cell in the table contains a number of values separated by commas. Some cells contain NULL (no value). I want to get the number total number of values in all the rows. (Using php & mysql)
Hence the total number of data should be 6, ie 2 in 1st row, 1 in 2nd row & 3 in 4th row.
The value count will be +1 than comma count. But if a column doesn't have comma, it should be zero. So this will work:
SELECT sum(LENGTH('col1') - LENGTH(REPLACE('col1', ',', ''))+1) AS total
FROM table1 WHERE LENGTH('col1')>0
Using php extract all the values in an array and use explode to get the total number of values.
$i=0;
while(mysqli_num_rows($result) >0)
{
while($row = mysqli_fetch_array($conn, $result))
{
$data_elements = explode(',',$row['data']); // extract all the comma seperated value in array
$i += count($data_elements); //count them all
}
}
echo $i; // these are your total number of values
hope it helped :)
This assumes comma indicates one value
SELECT sum(LENGTH('field1') - LENGTH(REPLACE('field1', ',', ''))+1)
AS comma_sum from table1
Test this and let me know how it goes
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.