Working on a project with CodeIgniter and Active Record.
I face an issue with a query :
I have 3 tables in my database which I would like not only to join but also to make countS on 2 of them. The tables are respectively linked with user_id, store_user_id, event_user_id fields
Table 1 : user
user_id
Table 2 : store
store_user_id
Table 3 : event
event_user_id
What I am trying to do is to :
1 - Get all the data from user
2 - Count the number of stores with store_user_id = user_id (it may be 0)
3 - Count the number of events with event_user_id = user_id (it may be 0)
I have done this in my function :
$this->db->select('*');
$this->db->select('COUNT(s.store_user_id) as total_store', FALSE);
$this->db->select('COUNT(e.event_user_id) as total_event', FALSE);
$this->db->from('user u');
$this->db->join('store s', 's.store_user_id = u.user_id', 'left'); // this joins the user table to store table
$this->db->join('event e', 'e.event_user_id = u.user_id', 'left'); // this joins the user table to event table
$this->db->group_by('u.user_id');
$q = $this->db->get();
if ($q->num_rows()>0){
foreach ($q->result() as $rows) {
$data[] = $rows;
}
return $data;
}
The trouble is that when I display total_store and total_event in my view, the results are the same and I think figures are multiplied between them..
For example :
For an user I have 3 events en 4 stores, the results displayed will be total_event = total_store = 12 ...
I don't understand why and it makes me crazy for hours!! Moreover, when I make only one count, the result is correct..
Any idea??
Many thanks in advance :)
Lastly I have implemented this basic SQL query :
$this->db->query('SELECT
u.*,
x.total_store,
y.total_event
FROM
user u
LEFT OUTER JOIN (SELECT s.store_user_id, COUNT(s.store_user_id) AS total_store
FROM store s
GROUP BY s.store_user_id) x ON x.store_user_id = u.user_id
LEFT OUTER JOIN (SELECT e.event_user_id, COUNT(e.event_user_id) AS total_event
FROM event e
GROUP BY e.event_user_id) y ON y.event_user_id = u.user_id
');
Hope it will helps others
When you count() you're counting the number of rows, not the number of distinct values in the result set. You're right that the number is being multiplied: there's a row in your resultset for each user-store-event combination.
Related
I am having a problem getting data from a large amount MySQL database.
With the below code it is ok to get the list of 10K patients and 5K appointments which is our test server.
However, on our live server, the number of patients is over 100K and the number of appointments is over 300K and when I run the code after a while it gives 500 error.
I need the list of the patients whose patient_treatment_status is 1 or 3 and has no appointment after one month from their last appointment. (The below code is working for small amount of patients and appointments)
How can I optimise the first database query so there will be no need the second database query in the foreach loop?
<?php
ini_set('memory_limit', '-1');
ini_set('max_execution_time', 0);
require_once('Db.class.php');
$patients = $db->query("
SELECT
p.id, p.first_name, p.last_name, p.phone, p.mobile,
LatestApp.lastAppDate
FROM
patients p
LEFT JOIN (SELECT patient_id, MAX(start_date) AS lastAppDate FROM appointments WHERE appointment_status = 4) LatestApp ON p.id = LatestApp.patient_id
WHERE
p.patient_treatment_status = 1 OR p.patient_treatment_status = 3
ORDER BY
p.id
");
foreach ($patients as $row) {
$one_month_after_the_last_appointment = date('Y-m-d', strtotime($row['lastAppDate'] . " +1 month"));
$appointment_check = $db->single("SELECT COUNT(id) FROM appointments WHERE patient_id = :pid AND appointment_status = :a0 AND (start_date >= :a1 AND start_date <= :a2)", array("pid"=>"{$row['id']}","a0"=>"1","a1"=>"{$row['lastAppDate']}","a2"=>"$one_month_after_the_last_appointment"));
if($appointment_check == 0){
echo $patient_id = $row['id'].' - '.$row['lastAppDate'].' - '.$one_month_after_the_last_appointment. '<br>';
}
}
?>
First off, this subquery likely does not do what you think it does.
SELECT patient_id, MAX(start_date) AS lastAppDate
FROM appointments WHERE appointment_status = 4
Without a GROUP BY clause, that subquery will simply take the maximum start_date of all appointments with appointment_status=4, and then arbitrarily pick one patient_id. To get the results you want you'll need to GROUP BY patient_id.
For your overall question, try the following query:
SELECT
p.id, p.first_name, p.last_name, p.phone, p.mobile,
LatestApp.lastAppDate
FROM
patients p
INNER JOIN (
SELECT patient_id,
MAX(start_date) AS lastAppDate
FROM appointments
WHERE appointment_status = 4
GROUP BY patient_id
) LatestApp ON p.id = LatestApp.patient_id
WHERE
(p.patient_treatment_status = 1
OR p.patient_treatment_status = 3)
AND NOT EXISTS (
SELECT 1
FROM appointments a
WHERE a.patient_id = p.patient_id
AND a.appointment_status = 1
AND a.start_date >= LatestApp.lastAppDate
AND a.start_date < DATE_ADD(LatestApp.lastAppDate,INTERVAL 1 MONTH)
)
ORDER BY
p.id
Add the following index, if it doesn't already exist:
ALTER TABLE appointments
ADD INDEX (`patient_id`, `appointment_status`, `start_date`)
Report how this performs and if the data appears correct. Provide SHOW CREATE TABLE patient and SHOW CREATE TABLE appointments for further assistance related to performance.
Also, try the query above without the AND NOT EXISTS clause, together with the second query you use. It is possible that running 2 queries may be faster than trying to run them together, in this situation.
Note that I used an INNER JOIN to find the latest appointment. This will result in all patients that have never had an appointment to not be included in the query. If you need those added, just UNION the results those found by selecting from patients that have never had an appointment.
Cannot figure out query for situation where I want to display only customers with unverified order but do not include customers who already have at least one verified order. One customer can have more records in DB since for every order also new record in customers table is made so the only way how to track specific user is by customer_number.
My DB structure (simplified):
customers:
id | customer_number
orders:
id | customer_id | isVerified
I would probably need to combine join and correlated queries (to search records in customers table for every customer_number and check isVerified column for false) which in the end could be really slow especially for thousands of records.
I use Laravel so Eloquent ORM is available if this can make things easier.
(Second thought: Or maybe it would be faster and more efficient to rewrite that part to create only one user record for orders of specific user.)
Any ideas? Thank you
There are probably a few ways to do this, but you can achieve this result with a join, aggregation and conditional sum:
select a.customer_id,
sum( case when isVerified = 1 then 1 else 0 end ) as Num_Verified,
sum( case when isVerified = 0 then 1 else 0 end ) as Num_unVerified
from customers as a
left join
orders as b
on a.customer_id = b.customer_id
group by a.customer_id
having Num_Verified = 0
and Num_unVerified > 0
SQLfiddle here
You can achieve like this:
$customer_id = Customer::join('orders','customers.id','orders.cutomer_id')
->where('isVerified',true)
->select('orders.*')
->groupBy('customer_id')
->pluck('customer_id');
This will give customers with at least one verified order.
Now get customers with unverified orders as:
$customers = Customer::join('orders','customers.id','orders.customer_id')
->where('isVerified',false)
->whereNotIn('customer_id',$customer_id)
->select('customers.customer_number','orders.*')
->groupBy('customer_id')
->pluck('customer_id');
How about this one?
$customer_list = customers::where('customer_number',$customer_number)->with('orders',function($query){
$query->where('isVerified',0);
})->get();
One method is an aggregation query:
select c.customer_number
from customers c join
orders o
on c.customer_id = o.customer_id
group by c.customer_number
having sum(isVerified = 1) = 0 and
sum(isVerified = 0) > 0;
This structure assumes that isVerified is a number that takes on the values of 0 for false and 1 for true.
Good Day!
Is it possible to apply if condition in the query in codeigniter: model?
I have this query in Model:
function activity($id) {
$this->db->select('a.ID, a.activityID, r.roleName');
$this->db->from('activity a');
$this->db->join('role r', 'r.ID = a.activityID');
$this->db->where('a.ID', $id);
$query = $this->db->get();
return $query->result_array();
}
And in my view I want to show the ID and activityID from table activity and also the counter part roleName (from table role) of activityID.
But the tricky part is that the value in activityID is not just a simple INT, like this:
ID ActivityID
1 5,7,9
2 2,4
3
4 10
5 1,6
So I'm thinking to use if condition in the query, like if the activityID value not contains comma (,) it will use join like this: $this->db->join('role r', 'r.ID = a.activityID'); then get the r.roleName.
Example:
ID ActivityID Role Name
4 10 Basketball
But if contains comma (,), it will explode by comma (,) then join like this: $this->db->join('role r', 'r.ID = a.activityID'); but it will output also by roleName with comma in between.
Example:
ID ActivityID Role Name
1 5,7,9 Tennis,Football,Soccer
2 2,4 Volleyball,Chess
Can someone help me with this?
Thanks in advance.
This is very bad idea to have comma separated ids in your column you should first normalize your structure other wise it will lead you more expensive problems,for now you use FIND_IN_SET() ,for having role names also as comma separated you can use GROUP_CONCAT
Be ware of that fact it has a default limit of 1024 characters to concat but it can be increased which is defined in manual
SELECT
a.ID,
GROUP_CONCAT(a.activityID) activityID,
GROUP_CONCAT(r.roleName) roleName
FROM activity a
LEFT JOIN `role` r ON(FIND_IN_SET(r.ID,a.activityID) > 0);
WHERE a.ID = $id
GROUP BY a.ID
For CI use query() function for above query
Please first have a look at Database Normalization
Select 3 rows from table1
Get a specific column data out of each row.
Then use that each column data obtained , to make a query again to get data from table2.
Store the data obtained in step 4 into a variable for each row.
Then put them in json array (table 1 , 3 rows + table 2's data(each of them).
I am building a rank table, it displays top 3 users with their rank name.
For example:
User1 has 2000 points , user 2 has 4000points , user 3 has 10k points , so the top 3 user is :
user 3 > user 2 > user 1
So , i want the php to go to 'users' table and get the top 3 members using this:
$query = mysql_query("SELECT * FROM users ORDER BY pts DESC LIMIT 3");
$rows = array();
while($r = mysql_fetch_assoc($query)) {
$rows[] = $r;
}
Table structure for 'user':
1.username(varchar)
2.pts(int)
After the rows are put into an array , how can i get 'points' for each of the row in that array.
Then go to 'rank' table to get their ranknames.
Table structure for 'rank':
1.rank(varchar)
2.pts(int)
Inside rank table there is 'pts' to let php choose compare which rank the user is at based on the points from each row of the array.
Normally i would use this if its only for 1 user , but for multiple users , im not sure:
$result = mysql_query("SELECT * FROM rank WHERE pts <= '$upts' ORDER BY pts DESC LIMIT 1")
or die(mysql_error());
Then after getting the rank for the top 3 users , php will now add the ranks to each of the user(row) in that array(of course , add it to the rank owner, not just simply place it in).
Then JSON encode it out.
How can i do this?
I am not sure if this is what you want. That is combine the two query into one query. Please take a look at http://sqlfiddle.com/#!2/ad419/8
SELECT user.username,user.pts,rank.rank
FROM user LEFT JOIN rank
ON user.pts <=rank.pts group by user.id
UPDATED:
For extracting top 3, could do as below;
SELECT user.username,user.pts,rank.rank
FROM user LEFT JOIN rank
ON user.pts <=rank.pts
GROUP BY user.id
ORDER BY pts DESC LIMIT 3
If i understand correctly, you need to get values from Rank and Users tables. In order to do that in just one query You need to add FK (Foreign Key) to the Rank table that points to a specific user in the Users table.
So you need to add userId to the Rank table and then you can run:
SELECT r.rank, u.points from users u,rank r where u.userId = r.userId
This is roughly what you need.
Not quite the answer to your exact question, but this might be of use to you: How to get rank using mysql query. And may even mean that you don't require a rank table. If this doesn't help, I'll check back later.
Use this query
$query = "SELECT
u.pts,
r.rank
FROM users as u
left join ranks as r
on r.pts = u .pts
ORDER BY pts DESC
LIMIT 3";
This will bring what you required without putting into an array
$rec = mysql_query($query);
$results = arrau();
while($row = mysql_fetch_row($rec)){
$results[] = $row;
}
echo json_encode($results);
It looks like what you're trying to do is retrieve the rank with the highest point requirement that the user actual meets, which isn't quite what everyone else is giving here. Fortunately it is easily possible to do this in a single query with a nice little trick:
SELECT
user.username,
SUBSTRING_INDEX(GROUP_CONCAT(rank.rank ORDER BY pts DESC),",",1) AS `rank`
FROM user
LEFT JOIN rank ON user.pts >= rank.pts
GROUP BY user.id
ORDER BY pts DESC
LIMIT 3
Basically what the second bit is doing is generating a list of all the ranks the user has achieved, ordering them by descending order of points and then selecting the first one.
If any of your rank names have commas in then there's another little tweak we need to add on, but I wouldn't have thought they would so I've left it out to keep things simple.
I have two tables, Table A and Table B. For each record in table A there are many records in Table B; thus, a one to many relationship exists between tables A and B. I want to perform a query so that for each row returned from table A, all of the corresponding rows will be returned from table B. From what I understand I'll need to use a INNER Join - however, how would I go about accessing all of the returned rows through say, PHP?
$sql = "Select A.ID, B.Name * From A INNER JOIN B ON A.ID = B.ID";
A.ID | B.fName | B.lName
1 nameone lnameone
1 nametwo lnametwo
2 namethree lnamethree
4 namefour lnamefour
Now that I have the above results, I want to use PHP to loop through all of the values of B.Name only for a single A.ID at a time. So, the results I want would look like:
1.
nameOne lNameOne
nameTwo lnametwo
2. namethree lNamethree
4. nameFour lNameFour
Basically, I'm trying to group the query results by the ID in table A.
I appreciate the help very much!
Thank you,
Evan
You could just Google "php get from database," and use some normal array pre-processing, but I worry the advice you find may not be ideal. Here's what I'd do:
$pdo = new PDO('mysql:host=host;dbname=dbname', 'user', 'pass');
$result = $pdo->query(<<<SQL
SELECT
ID,
Name
FROM
A
-- Alternative to `JOIN B USING(ID)` or `JOIN B ON (A.ID = B.ID)
NATURAL JOIN B
SQL
);
$a = array();
while ($row = $result->fetch()) {
if (!isset($a[$row['ID']]) {
$a[$row['ID']] = array();
}
$a[$row['ID']][] = $row['Name'];
}
You could also GROUP BY the ID and GROUP_CONCAT the names to be exploded in PHP later to skip the manual array creation and reduce some iteration (although SQL will do more in that case).
Adding a simple ORDER BY A.ID to the SQL query would probably get you quite far in "grouping" the items together. It's difficult to give a more detailed answer without knowing exactly what you want to do with the "groups".