I have a table like the above.
I have two variable, i.e. targetKey and targetCount.
I like to get how many records have the targetKey with targetCount.
When the targetKey is “is” and the targetCount is “2”,
The recordCount will be “1”. because The record (3) has “is” 2 times like the following.
When the targetKey is “is” and the targetCount is “3”,
The recordCount will be “2”. because The record (1) and (2) have “is” 3 times like the following.
When the targetKey is “city” and the targetCount is “2”,
The recordCount will be “0”. because The record (1) has 3 times and (3) have “city” just 1 time.
However the targetKey is “city” and the targetCount is “3”
The recordCount will be “1”.
Since I don’t know how to call the recordCount in mySQL, I made the PHP code below for it.
$testLD="SELECT n, say FROM myTable";
$testLT=mysqli_query($DBconnect, $testLD);
$targetKey='city'; $targetCount=3;
$keyLen=strLen($targetKey); $index=0;
$testL='<hr><hr>';
while ( $testLV=mysqli_fetch_assoc($testLT) ) {
$sayLen=strLen($testLV['say']);
$sayLen_without_key = strLen( str_replace($targetKey, '', $testL2V['say']) );
$lenDiffer=$sayLen-$sayLen_without_key;
$noKeyCount=$lenDiffer/$keyLen;
if ($noKeyCount == $targetCount ) {
$index++;
$testLV['say']=str_replace($targetKey, '<div class="ilb red">' .$targetKey . '</div>', $testLV['say']);
}
$testL=$testL.'(' .$testLV['n']. ') ' .$testLV['title']. '<br>' .$testLV['say']. '<br>
(sayLen='.$sayLen.') (' .$sayLen_without_key. ') (' .$lenDiffer. ')' .$noKeyCount. ' <hr>';
}
echo $testL.' ' .$index;
I like to get it in mySQL instead of PHP.
The following would -be SQL doesn’t work correctly but I hope it shows what I want.
SELECT count(*) as recordCount FROM myTable
WHERE say = ($targetKey, $targetCount time)
I expect the would-be result below of the SQL above
Can I get my target result above(when $targetKey is “city” and $targetCount is “3”) in SQL instead of PHP with your help?