I am using CodeIgniter and trying to query data from mysql.
Scenario -- Logged on user is Manager and wants to pull data for his team..
Explanation --
e_users table
|user_id | name | manager_id
---------------------------------
|1 | test | 10
|2 | another | 1
|3 | testX | 1
|4 | testY | 2
|5 | testX | 2
e_details table
|det_id | detail | user_id
---------------------------------
|1 | sdfs | 1
|2 | xcffgdf | 2
|3 | sdfsd | 3
|4 | etret | 5
|5 | cvbcvb | 5
Now when user_id 1 is logged as manager, he should get data of det_id -2,3 only,
e_details table have data and i want to query only those data of users where whose manager is currently logged on. Means manager will pull data of users from e_details table.
e_details table have user_id, detail_comment, details_date...
and e_users have user_id, manager_id , ...
I tried below function, but i could not get required data
$user_id = $this->session->userdata('user_id')
public function get_method($user_id)
{
$this->db->join('e_users', 'e_users.manager_id = e_details.user_id','INNER');
$this->db->where('e_details.user_id',$user_id);
return $this->db->get('e_details')->result_array();
}
I think i need to join two tables and then search with common key value?
Thanks,
Well If I am getting it correct you have missed the select statement.
This would help you
<?php
public function get_method($user_id){
$query = $this->db->select('e_users.*, e_details.*')
->from('e_users')
->join('e_details', 'e_users.user_id = e_details.user_id', 'inner')
->where('e_users.manager_id',$user_id)
->get();
if($query -> num_rows() > 0){
$details_array = $query->result();
print_r($details_array);die;
$response['details_array'] = $details_array;
return $response;
}else{
return false;
}
}
?>
Related
I have this db called Enrolment where all the students will be added with batch and course. stud_id, course_id, and batch_id are referring to the other table with the fk.
enrolment
+----------+---------+-----------+----------+
| enrol_id | stud_id | course_id | batch_id |
+----------+---------+-----------+----------+
| 1 | 1 | 2 | 1 |
| 2 | 5 | 3 | 2 |
| 3 | 4 | 5 | 3 |
+----------+---------+-----------+----------+
Before I delete the student record in the student table, i want to compare the stud_id in the students table with the stud_id in the enrollment table. If it matches, do not delete the student. Or some kind of message to say this record is linked with the enrollment table, or something.
If it matches i want to run this $sql = "DELETE FROM students WHERE student_id = $id ";
students
+---------+------------+-----------+
| stud_id | first_name | last_name |
+---------+------------+-----------+
| 1 | John | Doe |
| 2 | Susy | Roberts |
| 3 | John | redneck |
+---------+------------+-----------+
Please help!!
It can be achieved using NOT IN
DELETE FROM students
WHERE student_id NOT IN (SELECT stud_id
FROM enrolment) AND student_id = $id
It can be achieved using NOT EXISTS
DELETE FROM students
WHERE NOT EXISTS(SELECT NULL
FROM enrolment
WHERE stud_id = $id) AND student_id = $id
you want to run a query like
$sql = SELECT COUNT(1) FROM enrolment WHERE stu_id = $id
Then check the contents for the number returned if > 0 then error.
I got the following code where i select a random value from my sql table but i want to stop it from selecting a duplicate.
// Call on a random style ID to display in rating window.
$resultSet = $conn->query("SELECT pictureID,userID FROM styles ORDER BY RAND() LIMIT 1");
while($rows = $resultSet->fetch_assoc() ){
$rateableUserID = $rows['userID'];
$rateablePictureID = $rows['pictureID'];
}
The goal is something like tinder in this case. You get a random picture (in tinder a random person) and vote but the already voted picture by then should not reapear in this selection from my table. My goal was to compare the selected value with the value in a users table where every one of those id's is being marked as already voted by him.
Has anyone an idea of how to achive something like that?
Best Regards!
You need to create a table for user perviews, that stores numbers that have been retrieved before for this user
Users Table
+------+--------+--------+
|USERID|USERDATA|USERDATA|
+------+--------+--------+
|1 | XXXX | XXXX |
+------+--------+--------+
|2 | AAAA | AAAA |
+------+--------+--------+
|3 | ZZZZ | ZZZZ |
+------+--------+--------+
Userviews table
+------+---------+
|USERID|pictureID|
+------+---------+
|1 | 1 |
+------+---------+
|1 | 2 |
+------+---------+
|2 | 3 |
+------+---------+
|3 | 3 |
+------+---------+
|3 | 2 |
+------+---------+
Picture Table
+----------+----+
|PICTUIREID|DATA|
+----------+----+
|1 |WWWW|
+----------+----+
|2 |EEEE|
+----------+----+
|3 |RRRR|
+----------+----+
And then your query will be
SELECT p.pictureID FROM Userviews uv
join picture p on p.PICTUIREID = uv.PICTUIREID
where uv.USERID <> (userID )
ORDER BY RAND() LIMIT 1"
I'm building a symptom checker. As the user picks symptoms, we suggest other symptoms from ailments that have the user selected symptoms in common until we can narrow down to a specific ailment. I have this table.
ailment_symptoms
+----+----------+-----------+
|id |ailment_id|symptom_id |
+----+----------+-----------+
|1 | 1 | 1 |
|2 | 1 | 2 |
|3 | 2 | 1 |
|4 | 2 | 3 |
|5 | 3 | 3 |
|6 | 3 | 2 |
|7 | 4 | 1 |
|8 | 4 | 2 |
+----+----------+-----------+
If I want to select ailment_ids of entries that have symptom_id 1 and 2, I use this self join query.
SELECT t1.ailment_id
FROM ailment_symptoms t1
JOIN ailment_symptoms t2 ON t1.ailment_id = t2.ailment_id
WHERE t1.symptom_id = '1'
AND t2.symptom_id = '2'
which will return
+----------+
|ailment_id|
+----------+
| 1 |
| 4 |
+----------+
How do I do it when there are more than two symptom_ids. I want to write a php code that will build for as many symptoms as the users enters. code should look like:
$user_symptoms = array($symptom_id_1, $symptom_id_2, $symptom_id_3); //as many symptom as the user picks
$query = "SELECT t1.ailment_id FROM ailment_symptoms t1";
foreach($user_symptoms AS $symptoms){
//here is where we construct the multiple self join and append it to $query
//please replace this comment with appropriate code
}
$query .= "WHERE ";
//loop through $user_symptoms and append t1.symptom_id = '$user_symptom[0]' AND '....'
Please help me replace the comments with the appropriate code.
You can also do this with aggregation. It might be easier for you to construct the queries like this, because it is easier to handle the additional attributes.
SELECT s.ailment_id
FROM ailment_symptoms s
WHERE s.symptom_id in (1, 2)
GROUP BY s.ailment_id
HAVING COUNT(DISTINCT s.symptom_id) = 2;
You just have to be sure that the "2" matches the number of elements in the list in the where clause. Then it will generalize to any number of symptoms.
I have the following three tables
+----------+ +--------+ +-------------+
| adv | | member | | removed_adv |
+==========+ +========+ +=============+
| id | | id | | id |
| group | | group | | adv_id |
| category | | email | | member_id |
| title | | pw | +-------------+
| path | +--------+
| duration |
+----------+
What I am trying to do is to get all the data from table adv, where the member table's group is equal to the adv table's group. If the member table's id is equal to the _removed_adv_ table's _member_id_, that data should be removed from the result set.
I am using CodeIgniter's Active Record.
Could someone please help me? I really don't know how to do this.
You can try this:
select * from adv join member
on adv.group=member.group
where member.id not in (select member_id from removed_adv)
For CodeIgniter:
$removed = // get removed records member_id to here;
$CI->db->select('ad');
$CI->db->from('adv ad');
$CI->db->join('mem m', 'ad.group=mem.group', 'left');
$CI->db->where_not_in('mem.id', $removed);
$query = $CI->db->get();
I have table, named "table_log".
Here is the structure
---------------------------------------
|id_log | user_id | login_date |
---------------------------------------
|1 | 1 |2014-09-02 14:58:53 |
|2 | 1 |2014-09-03 24:18:53 |
|3 | 1 |2014-09-02 14:58:53 |
|4 | 1 |2014-09-01 02:28:53 |
|5 | 2 |2014-09-04 01:48:53 |
|6 | 3 |2014-09-05 04:58:53 |
|7 | 2 |2014-09-06 03:58:53 |
----------------------------------------
I want to count number of user each days. not how much log is.
As an example data, I want to show it like this:
---------------------------
|date | user_number|
---------------------------
|2014-09-02 | 1 |
|2014-09-03 | 1 |
|2014-09-04 | 5 |
---------------------------
Does any can help me? How to query my database?
SELECT date(login_date) AS date, count(DISTINCT user_id) AS user_count
FROM table_log
GROUP BY date(login_date)
The date function gives just the date-part of a datetime column, then it's a simple group by.
I dont get your ques, but u expect it?
2014-09-03 there is one record how user number will come 4 ? which scenario u asking?
SELECT date(login_date) AS date, count(date(login_date))
FROM tbl
GROUP BY date(login_date)