count number of characters of a particular field in a databse - php

The below shows my table users and some entries in the table. In users table phoneid is stored in particular manner as shown below .Some values are above 16 and some others are below 16 .
I want to display phoneid into two categories (One is above 16 & another one is below 16).
How can I categorize it into two category ???
phoneid
43f46cc43f4c797c
03C03372-B549-4FA2-9A6B-E28F250E8DWE
4f7e35b38470abd0
74C03372-B549-4FA2-9A6B-E28F250E8EBA
bbc4fe41d79aa730
61152d549ba924fa
c3ac44f7c0cd2a62
03C03372-B549-4FA2-9A6B-E28F250E8DWE
d07e063ef6d4598e
2f3467d189e6a8ac
fc35ee8d18f03b9e
189cc387b2b3ce3f
84B03372-B549-4BN2-9A6B-E28F250E8OPJ
Thanks in advance

If you need just to list the users and order them according to their length
you can simply run
SELECT * FROM tableName ORDER BY length(phoneid);
but if you need to get for example the ones with length greater than 16 just do this :
SELECT * FROM tableName WHERE length(phoneid) > 16;

you should use.. strlen() function of php..
which will return the character count of a string..
like
echo strlen($phoneid);

The length function in MySQL can take care of that.
SELECT * FROM users WHERE LENGTH(phoneid) > 16;

I think so this is what you are looking for:
SELECT * FROM tableName GROUP BY length(phoneid);

I want to display phoneid into two categories (One is above 16 & another one is below 16). How can I categorize it into two category ???
You can add temp field to your result sets, as below, to show the phone ids as defined categories based on their char lengths.
Select *,
case when char_length( phoneid ) <= 16 then
phoneid else '' end as 'Phone_Category_1',
case when char_length( phoneid ) > 16 then
phoneid else '' end as 'Phone_Category_2'
From users
Sample Result would be: (rest of the columns not shown here):
phoneid Phone_Category_1 Phone_Category_2
43f46cc43f4c797c 43f46cc43f4c797c
03C03372-B549-4FA2-9A6B-E28F250E8DWE 03C03372-B549-4FA2-9A6B-E28F250E8DWE
4f7e35b38470abd0 4f7e35b38470abd0
74C03372-B549-4FA2-9A6B-E28F250E8EBA 74C03372-B549-4FA2-9A6B-E28F250E8EBA
You can omit phoneid from result set and can depend on phone_category_1 and phone_category_2.

Related

SQLite Find missing numbers between range (3000-4000)

I'm looking to find the lowest missing number between 2 numbers.
there may be jumps between 3080 and 3085, etc and all I would need is 3081.
This is for assigning an id to an entry and these entries can be deleted thus the id is too and because we have a limited range we want to make sure we use all the numbers. So if there aren't any missing numbers it would have to grab the next available number. if it's full till 3850 then it would need to grab 3851.
They are 0 if they have been deleted rather than null if that helps.
SELECT number, user_id
FROM entries
WHERE user_id = '18'
AND number BETWEEN '3000' AND '4000'
I'm not sure how to get the lowest available number in this sequence, please help. Thank you.
Try using a self join:
SELECT MIN(e1.number) + 1 num_missing
FROM entries e1
LEFT JOIN entries e2
ON e2.number = e1.number + 1
WHERE e1.id = 18 AND e1.number BETWEEN 3000 AND 4000 AND
e2.number IS NULL;
The critical condition in the WHERE clause is e2.number IS NULL, which means that this particular number had no next immediate value in the sequence, i.e. it is the start of a gap.
If you want next missing number for an id, then:
select e.id, e.number + 1
from entries e
where not exists (select 1
from entries e2
where e2.id = e.id and e2.number = e.number + 1
);
If you want this for a particular id, then add id = 18 to the outer where clause.

Search Two Tables & Concatenate Answer

I am trying to search two tables, match the results and then concatenate the answer... Only finding results >= today's date. This will then give the user the option to delete the selected from the DB. So...
Table 1 called Prog_name
id prog_name
1 Breakfast
2 Mid Morning
3 Afternoon
Table 2 called talk_ups
id date_tx prog_name (prog_name value = prog_name.id)
1 2017-06-30 2
2 2017-07-03 1
3 2017-07-01 3
The result I am after is something like: "01-07-2017, Afternoon". But I do also need the talk_ups.id to ensure it only deletes the correct record.
I managed to figure out how to get the name to match the talk_ups.prog_name value:
'$sql. = "SELECT talk_ups.prog_name, prog_name.id as progID, prog_name.prog_name as theName FROM prog_name, talk_ups WHERE talk_ups.prog_name = prog_name.id";'
But I can't figure out how to do the two searches and end up with the right result and how to separate out the results to then concatenate them.
You can use JOIN with WHERE condition, e.g.:
SELECT pn.id, pn.prog_name, tu.date_tx
FROM prog_name pn JOIN talk_ups tu ON pn.id = tu.prog_name
WHERE tu.date_tx > NOW();

select different MAX(values) depending on array

I want to select different MAX(values) of different sensornodes. My Problem is that the WHERE...IN... clause works like many logical "OR", but I need the MAX($measure) from each sensornode. I know how to do it with a loop, but I think there is a better solution to do this.
This is the table 'measurement' of the database:
ID humidity temperature date time sensornode
---------- ---------- ----------- ---------- ---------- ----------
1 22.00% 18.00C 06/03/2017 13:07:18 WSN1
2 22.00% 18.00C 06/03/2017 13:08:19 WSN2
3 34.00% 21.00C 06/03/2017 13:09:19 WSN3
4 21.00% 20.00C 06/03/2017 13:10:19 WSN4
The query should be somthing like this
$measure //is either 'temperature' or 'humidity', depends on the users input.
$sensornode // is a string which is converted with the implode-function from an array which includes the selected 'sensornodes' of the users input
$sql_query = "SELECT MAX($measure) AS $measure
FROM measurement
WHERE sensornode IN ('$sensornode')";
$data = executeQuery($sql_query, $measure);
To get the maximum value for each sensor node, use grouping:
SELECT sensornode,
max(temperature)
FROM measurement
GROUP BY sensornode;
To restrict that to certain sensor nodes, just add a WHERE:
SELECT sensornode,
max(temperature)
FROM measurement
WHERE sensornode IN ('WSN1', 'WSN2', ...)
GROUP BY sensornode;

MySQL - How to get counts of specific value from all the records

I have course table and I am storing completion status of users with 'A'. Now I want to get how many A is available from CompletionStatus field for all records.
I want this result: A = 5.
Course Table:
CourseRecordIdx User CompletionStatus
--------------- ---- --------------------
1 152 A___A_______________
2 147 AA_______A__________
I have tried with char_length but getting count with underscore and I want to get only total of A:
SELECT char_length(CompletionStatus) FROM `course` where CourseRecordIdx = 36
Any idea how to get result with select query?
Thanks.
You can try with this simplest way:
SELECT length(REPLACE("field_name","_","")) FROM `tbl_name`;
You can use LENGTH and REPLACE for that purpose :
SELECT LENGTH(CompletionStatus) - LENGTH(REPLACE(CompletionStatus, 'A', '')) as count_Char
FROM `course`
This basically counts how many characters are in that string, and then checks the difference between that number, and the number of characters with out the specific character.
I suppose the following will work, first replace all _ with ''.
After that use char_length() function.
SELECT CHAR_LENGTH(REPLACE(CompletionStatus,'_','') as totalA FROM course where CourseRecordIdx = 36;
You can write this query for this:
SELECT CourseRecordIdx,User,CompletionStatus,
ROUND (
(
LENGTH(CompletionStatus)- LENGTH( REPLACE ( CompletionStatus, "A", ""))
) / LENGTH("A")) AS count
from `course` where CourseRecordIdx = 36

MYSQL : How to set to same position if row value is the same?

position | Average | gpmp
1 70.60 2.0
2 60.20 2.3
3 59.80 4.8
4 59.80 4.8
5 45.70 5.6
Hie All,
As above table, I need to arrange the position according to the lowest gpmp and the highest average. But when the both average and gmp are the same, I will need to have the position to be the same.
For example, position 3 and 4 have the same average and gpmp. How do I generate the mysql query or using php function so that after they detect the same average and gpmp and change the position 4 to 3.
Which mean after the function is generated it will become like the table below.
position | Average | gpmp
1 70.60 2.0
2 60.20 2.3
3 59.80 4.8
3 59.80 4.8
5 45.70 5.6
Here's a simple way to update the table as you described in your post - taking the sequential positions and updating them accordingly. It doesn't calculate the positions or anything, just uses the data already there:
UPDATE `table` t SET position = (
SELECT MIN(position) FROM (SELECT * FROM `table`) t2 WHERE t.Average = t2.Average AND t.gpmp = t2.gpmp
)
I'd give something like the following a try, through it does assume a primary key is on this table. Without a primary key you're going to have issues updating specific rows easily / you'll have a lot of duplicates.
So for this example I'll assume the table is as follows
someTable (
pkID (Primary Key),
position,
Average,
gpm
)
So the following INSERT would do the job I expect
INSERT INTO someTable (
pkID,
position
)
SELECT
someTable.pkID,
calcTable.position
FROM someTable
INNER JOIN (
SELECT
MIN(c.position) AS position,
c.Average,
c.gpm
FROM (
// Calculate the position for each Average/gpm combination
SELECT
#p = #p + 1 AS position,
someTable.Average,
someTable.gpm
FROM (
SELECT #p:=0
) v,someTable
ORDER BY
someTable.Average DESC,
someTable.gpmp ASC
) c
// Now regroup to get 1 position for each combination (the lowest position)
GROUP BY c.Average,c.gpm
) AS calcTable
// And then join this calculated table back onto the original
ON (calcTable.Average,calcTable.gpm) = (someTable.Average,someTable.gpm)
// And rely on the PK IDs clashing to allow update
ON DUPLICATE KEY UPDATE position = VALUES(position)
(pseudo code)
select * from table
get output into php var
foreach (php row of data)
is row equal to previous row?
yes - don't increment row counter, increment duplicate counter
no - increment row counter with # of duplicates and reset duplicate counter
save current row as 'previous row'
next
you can try something like this in php:
$d= mysql_query('select distinct gpmp from tablename order by gpmp');
pos= 1;
while($r= mysql_fetch_array($d)){
mysql_query('update tablename set position='.$pos.' where gpmp='.$r['gpmp']);
$pos++;
}
You only need to "expand" the idea to take averange in account too.

Categories