join select and count using codeigniter and mysql - mysql

I have three table as follow:
tblHelpdeskType (It need to join with tblHelpdesk)
HelpdeskTypeID | HelpdeskType | Current
1 | Not Specified | -1
2 | Feature Request | -1
3 | Account Request | -1
4 | Permission Requested | -1
5 | Bug | -1
tblHelpdesk (primary table)
HelpdeskID||HelpdeskTypeID||HelpdeskPriorityID||RequestBy||DateTimeStamp||HelpdeskStatusID
1 || 1 || 2 ||sothorn || 12/12/12 || 1
2 || 3 || 1 ||Chantra || 12/12/12 || 1
3 || 1 || 1 ||Lyhong || 12/12/12 || 2
4 || 3 || 1 ||bongthom || 12/12/12 || 1
tblHelpdeskStatus (It need to join with tblHelpdesk )
HelpdeskStatusID | HelpdeskStatus
1 | Pending
2 | View
3 | In treatment
4 | Closed
Result that I need is :
Type | Pending | View | In treatment | Closed |
----------------------------------------------------------------|
Not Specified | 1 | 1 | | |
----------------------------------------------------------------|
Feature Request | 2 | | | |
----------------------------------------------------------------|
Account Request | | | | |
----------------------------------------------------------------|
Permission Requested | | | | |
----------------------------------------------------------------|
Bug | | | | |
----------------------------------------------------------------|

Related

how do i remove duplicates from each column and still keep all the data in mysql ? its it possible ? pls help

SELECT
star_mss2_VideoMaster.Video_Title,
star_vlss_vid-location.video_location,
star_alss_aud-location.audio_location,
star_vss_VideoFile.Video-filename,
star_Afss_AudioFile.audio-filename,
star_stss_SvToLoctn.S_Location,
star_wses_ws_filename.wondershare_fileName,
star_wses_ws_filename.vid_length
FROM
star_mss2_VideoMaster`
JOIN
star_vlss_vid-location ON star_vlss_vid-location.video_title_id=star_mss2_VideoMaster.Video_id
JOIN
star_alss_aud-location ON star_alss_aud-location.video_title_id=star_vlss_vid-location.video_title_id
JOIN
star_vss_VideoFile ON star_mss2_VideoMaster.Video_id=star_vss_VideoFile.video_title_id
JOIN
star_Afss_AudioFile ON star_Afss_AudioFile.video_title_id=star_mss2_VideoMaster.Video_id
JOIN
star_stss_SvToLoctn ON star_mss2_VideoMaster.Video_id=star_stss_SvToLoctn.video_title_id
JOIN
star_wses_ws_filename ON star_wses_ws_filename.saveto_id=star_stss_SvToLoctn.st_id
A simpler version:
Table 1
+------+----=----+
| v_id | a_Title |
+------+-----==--+
| 1 | abc |
| 2 | abcs |
-----------------+
Table 2
+-----+-------+-------+
| sid | Title | v_id |
+-----+-------+-------+
| 1 | abc | 2 |
| 2 | abcs | 2 |
--------------+-------+
table 3 : Select * from table 1
JOIN Table 2 using (v_id);
+------+------------++-----+-------+---------+
| v_id | a_Title || sid | Title | v_id |
+------+-----------++-----+-------+---------+
| 2 | abcs || 1 | abc | 2 |
---------------------+-----------------------
| 2 | abcs || 2 | abcs | 2 |
--------------------+--------------+---------+
I don't like this output. There are two abcs under a_Title.
I want output like:
+------+---------++-----+-------+
| v_id | a_Title || sid | Title |
+------+---------++-----+-------+
| 2 | abcs || 1 | abc |
-----------------+---------------
| | || 2 | abcs |
-----------------+--------------+

Skip First consecutive records which has same values, then stop

This is my table scenario
+----------+------------+---------------------+------------------+
| userd_id | user_value | user_datetime | i_want_to_select |
+----------+------------+---------------------+------------------+
| 1 | 1 | 2020-01-31 12:13:14 | |
| 2 | 1 | 2020-01-30 12:13:14 | |
| 3 | 1 | 2020-01-29 12:13:14 | |
| 4 | 2 | 2020-01-28 12:13:14 | |
| 5 | 2 | 2020-01-27 12:13:14 | |
| 6 | 3 | 2020-01-20 12:13:14 | |
| 7 | 1 | 2020-01-19 12:13:14 | this |
| 8 | 1 | 2020-01-18 12:13:14 | this |
| 9 | 1 | 2020-01-17 12:13:14 | this |
+----------+------------+---------------------+------------------+
The need is to skip those consecutive values, only IF first element is 1, ​​and stop as soon as the value is different
I do it in PHP code, but I want to avoid weighing down the PHP processor and the garbage collector
foreach ($rows as &$row){
if($row['user_value'] === 1){
unset($row); // remove my row
} else {
break; // at the first different value, stop
}
}
unset($row);
$rows = array_values($rows) //reset the array index/key
Thanks
One method is:
select t.*
from t
where t.user_datetime >= (select min(t2.user_datetime)
from t t2
where t2.user_id = t.user_id and
t2.user_value <> 1
);
You can also use window functions, for instance:
select t.*
from (select t.*,
min( case when t.user_value <> 1 then t user_datetime) over (partition by t.user_id) as min_not1_datetime
from t
) t
where user_datetime >= min_not1_datetime;

Update one Table Using another Table Record

I have two tables as follows that joined by budget_id.
finance_budget table
+-----------+--------+------------+-----------------+---------------+
| budget_id | amount | date | transfer_status | budget_status |
+-----------+--------+------------+-----------------+---------------+
| 1 | 135000 | 2019-10-01 | Pending | issue |
| 2 | 25000 | 2019-10-02 | Pending | issue |
| 3 | 234000 | 2019-10-03 | Pending | issue |
| 4 | 175000 | 2019-10-03 | Pending | issue |
+-----------+--------+------------+-----------------+---------------+
finance_budget_issue table
+----+-----------+-----------+--------+-----------------+---------------+
| id | budget_id | office_id | amount | transfer_status | budget_status |
+----+-----------+-----------+--------+-----------------+---------------+
| 1 | 1 | 100 | 135000 | Pending | issue |
| 2 | 2 | 101 | 12500 | Pending | issue |
| 3 | 2 | 102 | 12500 | Pending | issue |
| 4 | 3 | 100 | 100000 | Pending | issue |
| 5 | 3 | 105 | 75000 | Pending | issue |
| 6 | 3 | 104 | 59000 | Pending | issue |
| 7 | 4 | 102 | 125000 | Pending | issue |
| 8 | 4 | 110 | 50000 | Pending | issue |
+----+-----------+-----------+--------+-----------------+---------------+
I tried to change the "transfer_status" of above two tables from "Pending" to "Approved" using my model
Controller
public function approveIssues($id){
$this->checkPermissions('index', 'pendingIssues');
if(empty($id)){
redirect('budget/pendingIssue');
}
if($this->Budget_model->approveIssues($id)){
$this->session->set_flashdata('message', 'Allocation Approved successfully ..!!');
redirect('budget/pendingIssue');
}else{
$this->session->set_flashdata('error', 'Error ...!!!');
redirect('budget/pendingIssue');
}
}
Model
function approveIssues($id)
{
$this->db->update('finance_budget_issue', array('transfer_status'=>'Approved'), array('id' => $id));
$this->db->update('finance_budget', array('transfer_status'=>'Approved'), array('budget_id' => $id));
if ($this->db->affected_rows()) {
$activity=FZ_Controller::activity('approve','finance_budget_issues',$id,NULL);
$this->db->insert('finance_user_activity',$activity);
return true;
}
return false;
}
When I press the Approved button in my view the system shows "Error". If I remove the line, $this->db->update('finance_budget', array('transfer_status'=>'Approved'), array('budget_id' => $id));
in my model the operation is executing successfully and shows the "Allocation Approved successfully ..!!" Message.
Desired Output
I also need the transfer_status of finance_budget table should be into "Approved".
You can try this Model Code
function approveIssues($id) {
$this->db->set('finance_budget.transfer_status','Approved');
$this->db->set('finance_budget_issue.transfer_status','Approved');
$this->db->where('finance_budget.budget_id', $id);
$this->db->where('finance_budget_issue.id', $id);
$this->db->update('finance_budget JOIN finance_budget_issue ON finance_budget.budget_id = finance_budget_issue.id');
if ($this->db->affected_rows()) {
$activity=FZ_Controller::activity('approve','finance_budget_issues',$id,NULL);
$this->db->insert('finance_user_activity',$activity);
return true;
}
return false;
}
i think it's very helpful for you.

mySQL: order by Parent and Child according to Weight

I have a "category" table like this:
+------+---------++---------+
| id | parent || weight |
+------+---------++---------+
| 1 | 0 || 0 |
| 2 | 1 || -1 |
| 3 | 1 || -2 |
| 4 | 2 || 0 |
| 5 | 1 || 1 |
| 6 | 5 || 0 |
| 7 | 5 || 1 |
+------+---------++---------+
How can I ORDER BY and SELECT statement to get the order like this:
1 5 7 6 2 4 3
in the first level every category in main branch (parent=1) get the order by their weights, while they have their children behinds.
thank You and sorry for my eng

How to select every level with different user and user progress

I wanted to make a learning apps with a completed questions progress track on each level. I have 2 tables which contain all the level with its question count, and a table that save an user progress (completed questions) on each level. Here is the record,
Level table:
+----------+----------------------+-------------+--------+
| id_level | name | jumlah_soal | id_sub |
+----------+----------------------+-------------+--------+
| 1 | Basic Level 1 | 5 | 1 |
| 2 | Basic Level 2 | 6 | 1 |
| 3 | Basic Level 3 | 7 | 1 |
| 8 | Intermediate Level 1 | 5 | 2 |
| 9 | Intermediate Level 2 | 5 | 2 |
| 10 | Intermediate Level 3 | 5 | 2 |
..........................................................
Progress Table:
+-----------------+---------+-------------+----------+
| id_progreslevel | id_user | completed | id_level |
+-----------------+---------+-------------+----------+
| 1 | 1 | 2 | 1 |
| 2 | 1 | 3 | 2 |
| 3 | 2 | 2 | 1 |
+-----------------+---------+-------------+----------+
and when I joined the tables with the following query
SELECT IFNULL(progreslevel.id_user, 1) as id_user,
-> level.*, IFNULL(progreslevel.completed, 0) as completedquestions
-> FROM level LEFT JOIN progreslevel
-> ON level.id_level = progreslevel.id_level
-> WHERE level.id_sub = 1
-> HAVING id_user = 1;
It queries what I wanted:
+---------+----------+---------------+-------------+--------+--------------------+
| id_user | id_level | name | jumlah_soal | id_sub | completed questions |
+---------+----------+---------------+-------------+--------+--------------------+
| 1 | 1 | Basic Level 1 | 5 | 1 | 2 |
| 1 | 2 | Basic Level 2 | 6 | 1 | 3 |
| 1 | 3 | Basic Level 3 | 7 | 1 | 0 |
+---------+----------+---------------+-------------+--------+--------------------+
BUT, when I tried to change to query a progress for user ID = 2, it became like this:
+---------+----------+---------------+-------------+--------+--------------------+
| id_user | id_level | name | jumlah_soal | id_sub | completed questions |
+---------+----------+---------------+-------------+--------+--------------------+
| 2 | 1 | Basic Level 1 | 5 | 1 | 2 |
| 2 | 3 | Basic Level 3 | 7 | 1 | 0 |
+---------+----------+---------------+-------------+--------+--------------------+
Yes, the Basic Level 2 is gone because user 2 hadn't done it yet but user 1 had.
This is where I'm stuck, I want to whichever user I choose, its always query all the level, even when the other user had done it. It should be like this:
+---------+----------+---------------+-------------+--------+--------------------+
| id_user | id_level | name | jumlah_soal | id_sub | completed questions |
+---------+----------+---------------+-------------+--------+--------------------+
| 2 | 1 | Basic Level 1 | 5 | 1 | 2 |
| 2 | 2 | Basic Level 2 | 6 | 1 | 0 |
| 2 | 3 | Basic Level 3 | 7 | 1 | 0 |
+---------+----------+---------------+-------------+--------+--------------------+
How do I achieve this?
Thanks in advance and Sorry if you got dizzy either at my explanation or database, I tried my best to translate it, so it's understandable
I'm not really sure that I understand the requirement, but I think you're after something like this...
SELECT l.*
, 2 id_user
, COALESCE(MAX(p.completed),0) completed_questions
FROM level l
LEFT
JOIN progress p
ON p.id_level = l.id_level
AND p.id_user = 2
WHERE l.id_sub = 1
GROUP
BY l.id_level;