I have this query that sums values from different columns when they have a id active like id_test = 1 note_test = 11, it has a case stament if the id is in 0
this is the query
select
case when id_test1 = 1 then note_test1 else 0 end +
case when id_test2 = 1 then note_test2 else 0 end +
case when id_test3 = 1 then note_test3 else 0 end +
case when id_test4 = 1 then note_test4 else 0 end +
case when id_test5 = 1 then note_test5 else 0 end +
case when id_test6 = 1 then note_test6 else 0 end +
case when id_test7= 1 then note_test7 else 0 end as total_weighted
from rp_student where id = 1
This is my query in laravel
$weighted= DB::table('rp_student')
->select(DB::raw('case when id_test1 = 1 then note_test1 else 0 end +
case when id_test2 = 1 then note_test2 else 0 end +
case when id_test3 = 1 then note_test3 else 0 end +
case when id_test4 = 1 then note_test4 else 0 end +
case when id_test5 = 1 then note_test5 else 0 end +
case when id_test6 = 1 then note_test6 else 0 end +
case when id_test7= 1 then note_test7 else 0 end as total_weighted'))
->where('id','=',1);
It doesn't return me any value
Your query is missing the required ->get() at the end. The reason why your don't see a value currently is the query hasn't actually been executed.
The get method returns an Illuminate\Support\Collection containing the results where each result is an instance of the PHP stdClass object. You may access each column's value by accessing the column as a property of the object:
https://laravel.com/docs/5.8/queries#retrieving-results
Related
I have confusion since I was looking inside and try to solve the problem. I have PHP application which is running in Oracle DB and so far some expression is written in MySQL and right now I want to migrate those query to Oracle SQL function and call function insted of writing query directly to model.
The situation is following
I have function line
public function getAtaNumber($data) {
if( $data["Type"] == 1 ) {
$sql = "SELECT SUM(NextNumber) + 1 AS NextNumber
FROM (
SELECT COUNT(a1.ATAID) AS NextNumber
FROM ata AS a1
WHERE a1.ProjectID = '".$data["project"]["id"]."'
AND a1.Ata = 1
AND a1.ParentAta = 0
AND a1.TYPE = 1
AND a1.BecomeExternalAtaFromInternal = 0
UNION ALL
SELECT COUNT(a2.ATAID) AS NextNumber
FROM ata AS a2
WHERE a2.ProjectID = '".$data["project"]["id"]."'
AND a2.Ata = 1
AND a2.ParentAta = 0
AND a2.BecomeExternalAtaFromInternal = 1 ) AS A";
} else {
$sql = "SELECT COUNT(ata.ATAID) + 1 AS NextNumber
FROM ata
WHERE ProjectID = '".$data["project"]["id"]."'
AND Ata = 1
AND ParentAta = 0
AND Type = 0
ORDER BY ATAID DESC
LIMIT 1;";
if (!$result = $conn->query($sql)) {
error_log($conn->error);
return false;
}
}
}
This if make me confusion and I have no idea how to write this peace of code. Since I run SELECT statment both of them and it return correct result, but I have no idea what to do with this IF
Since this is two dimension array and I have no idea how to write it in PLSQL Oracle SQL
When I run the first SELECT statment
SELECT SUM(NextNumber) + 1 AS NextNumber
FROM (
SELECT COUNT(a1.ATAID) AS NextNumber
FROM ata a1
WHERE a1.ProjectID = 137
AND a1.Ata = 1
AND a1.ParentAta = 0
AND a1.TYPE = 1
AND a1.BecomeExternalAtaFromInternal = 0
UNION ALL
SELECT COUNT(a2.ATAID) AS NextNumber
FROM ata a2
WHERE a2.ProjectID = 137
AND a2.Ata = 1
AND a2.ParentAta = 0
AND a2.BecomeExternalAtaFromInternal = 1
) A
NEXTNUMBER
18
And When I run second SELECT query
SELECT COUNT(ata.ATAID) + 1 AS NextNumber
FROM ata
WHERE ProjectID = 137
AND Ata = 1
AND ParentAta = 0
AND Type = 0
ORDER BY ATAID DESC ;
NEXTNUMBER
1
Can someone tell me how to solve this two dimension array problem in PLSQL Oracle SQL or Does even this kind of array exist in Oracle SQL ?
Just one notation I also try something like
IF(data"Type"==1)
SELECT
ELSE
SELECT
Also
IF(data'Type'=1)
SELECT
ELSE
SELECT
Here is a function which returns the next ATA Number for a given PROJECT_ID and TYPE.
create or replace function get_ata_number (
p_project_id in number
,p_type in number
) return number as
l_NextNumber number;
begin
if p_type = 1 then
SELECT SUM(NextNumber) + 1
into l_NextNumber
FROM (
SELECT COUNT(a1.ATAID) AS NextNumber
FROM ata AS a1
WHERE a1.ProjectID = p_project_id
AND a1.Ata = 1
AND a1.ParentAta = 0
AND a1.TYPE = 1
AND a1.BecomeExternalAtaFromInternal = 0
UNION ALL
SELECT COUNT(a2.ATAID) AS NextNumber
FROM ata AS a2
WHERE a2.ProjectID = p_project_id
AND a2.Ata = 1
AND a2.ParentAta = 0
AND a2.BecomeExternalAtaFromInternal = 1 );
else
SELECT COUNT(ata.ATAID) + 1
into l_NextNumber
FROM ata
WHERE ProjectID = p_project_id
AND Ata = 1
AND ParentAta = 0
AND Type = 0 ;
end if;
return l_NextNumber;
end get_ata_number;
Note my assumption about the data type of PROJECT_ID, and correct it if necessary.
i have a table "tbl_userpersonal",
what i want to achieve is check which columns are filled and which are not so i can calculate or display the profile completion percentage.
\
So far i've tried a lot of different techniques and codes and my code is counting all columns but i want it to count empty value columns as 0.
please help me with a solutions.
So far if there is even a single entry for a user in any column it's giving me 100% otherwise 0% there is no inbetween
Currently if there is value inside "father_name" & "mother_name" & "DOB" column for a user with user_authtoken = "app_7837hfjd57hdj" the expected output should be
3
50% i.e (3/6)*100
what is happening right now is
if there is value inside either of these columns for a user with user_authtoken = "app_7837hfjd57hdj" the output is showing as
6
100% i.e (6/6)*100
or if there is no entry for user with user_authtoken = "app_7837hfjd57hdj" the output is giving
0
0%
here is the php code
$personal = mysqli_query($con,"
SELECT father_name,
mother_name,
DOB,
adhar_no,
address,
religion,
CASE WHEN father_name IS NOT NULL THEN 1 ELSE 0 END +
CASE WHEN mother_name IS NOT NULL THEN 1 ELSE 0 END +
CASE WHEN DOB IS NOT NULL THEN 1 ELSE 0 END +
CASE WHEN adhar_no IS NOT NULL THEN 1 ELSE 0 END +
CASE WHEN address IS NOT NULL THEN 1 ELSE 0 END +
CASE WHEN religion IS NOT NULL THEN 1 ELSE 0 END AS personal
FROM user WHERE `user_authtoken` = '$user_ath'
") or die(mysqli_error($con));
$data=mysqli_fetch_assoc($personal);
$pro_count = $data['personal'];
echo $pro_count ;
echo '<br>';
$percentage = ($pro_count /6)*100;
echo $percentage;
i think you have empty value in table not null try this
this will check for null as well as empty
$personal = mysqli_query($con,"
SELECT father_name,
mother_name,
DOB,
adhar_no,
address,
religion,
CASE WHEN father_name IS NOT NULL AND father_name<>'' THEN 1 ELSE 0 END +
CASE WHEN mother_name IS NOT NULL AND mother_name<>'' THEN 1 ELSE 0 END +
CASE WHEN DOB IS NOT NULL AND DOB<>'' THEN 1 ELSE 0 END +
CASE WHEN adhar_no IS NOT NULL AND adhar_no<>'' THEN 1 ELSE 0 END +
CASE WHEN address IS NOT NULL AND address<>'' THEN 1 ELSE 0 END +
CASE WHEN religion IS NOT NULL AND religion<>'' THEN 1 ELSE 0 END AS personal
FROM user WHERE `user_authtoken` = '$user_ath'
") or die(mysqli_error($con));
I can't figure-it out how to count all these dancers columns and echo with total of all dancers
id | dancer1 | dancer2 | dancer3 | dancer4, and so on..
---------------------------------------
1 alex michael dalm name
2 clare rose test
I have this for the start but is not working:
$counter = mysql_query("SELECT COUNT(*) AS id FROM table");
$num = mysql_fetch_array($counter);
$dancers = $num["id"];
echo "Total dancers: $dancers";
Any help is appreciated. Thanks!
Try this:
$counter = mysql_query("SELECT * FROM table");
$dancers = 0;
while($rows = mysql_fetch_array($counter)){
for($i = 1; $i <= 24; $i++){
$dan_id = 'dancer'.$i;
if($rows[$dan_id] != "" || $rows[$dan_id] != null )
$dancers++;
}
}
echo "Total dancers:". $dancers;
Note: Never design your database table like this.
I would actually save your dancers in a different (easier) way... For examle:
ID NAME SURNAME PHONE ....
1 Anna Brickstone 0975 ...
2 Jef Damen 0754 ...
That way you could use the following code to count tables:
$dancersCount="0";
$sql = "SELECT * FROM dancers";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$dancersCount++;
}
} else {
echo "No dancers..";
}
echo"$dancersCount";
The counter will count +1 each time it finds a row (dancer)
If you really want to do it that way...
Then I don't really think there's an easy way to fix this... You will probably need to check how many columns you have in your database but that's not something i can help you with...
You need to change your table structure:
id | dancerNumber | name
1 1 alex
2 1 clare
3 2 michael
4 2 rose
5 3 dalm
6 3 test
7 4 name
8 4 dana
SELECT COUNT(*) AS id FROM table will return 8 dancers. If this is what you were looking for?
if you want to keep your structure then you need to do the following sql query
SELECT dancer1,
dancer2,
dancer3,
(CASE WHEN (dancer1 <> "" AND dancer1 IS NOT NULL) THEN 1 ELSE 0 END +
CASE WHEN (dancer2 <> "" AND dancer2 IS NOT NULL) THEN 1 ELSE 0 END +
CASE WHEN (dancer3 <> "" AND dancer3 IS NOT NULL) THEN 1 ELSE 0 END) AS COUNTER
FROM table
This will count all the non empty and non null columns and add a counter at the end of the table. this counter will then contain the number of dancers with your structure.
Full answer with your php code
$query = 'SELECT (CASE WHEN (dancer1 <> "" AND dancer1 IS NOT NULL) THEN 1 ELSE 0 END +
CASE WHEN (dancer2 <> "" AND dancer2 IS NOT NULL) THEN 1 ELSE 0 END +
CASE WHEN (dancer3 <> "" AND dancer3 IS NOT NULL) THEN 1 ELSE 0 END) AS COUNTER
FROM table'
$counter = mysql_query($query);
$num = mysql_fetch_array($counter);
$dancers = $num["COUNTER"];
echo "Total dancers: $dancers";
In my query I want to discard the values equals to 0, actually this is my query:
$query = $this->db->select('GroupID')
->group_by('GroupID')
->from('ea_appointments')
->where('id_users_provider', $record_id)
->get()->result_array();
how you can see this query return only the GroupID value not equals so if I've:
0 - 0 - 1 - 1 - 2
(as GroupID).
I get only:
0 - 1 - 2
now I want to get only the value greater than 0, so thr query should be return this:
1 - 2
how I can achieve that in CodeIgniter?
just add a where statement for the GroupID and include the > symbol at the end of the column name like so:
$query = $this->db->select('GroupID')
->group_by('GroupID')
->from('ea_appointments')
->where('id_users_provider', $record_id)
->where('GroupID >', 0)
->get()->result_array();
id state
1 1
2 0
5 1
4 0
7 1
I want first remove all state=0 form my result. and then return row number of target record.
for example for id='7' it must return me 3
how can I do it with mysqli/php?
this is false but something like this:
function getLogs($itime = 0,$count = null)
{
$conn = $this->connectDB();
$sql = "SELECT num_rows() FROM Content Where id='22' and $state=0 ORDER BY id ASC";
$rows = mysqli_fetch_all($conn->query($sql), MYSQLI_ASSOC);
$this->disconnectDB($conn);
return num_rows();
}
This is probably simpler and you can replace 22 with a variable.
SELECT COUNT(id) FROM Content WHERE id <= 22 AND state != 0
In your example you put 22 in quotes, but your id isn't a string is it? Plus in addition to the comment above about $state = 0, it should be state not $state