I have two tables, users and clients_closed_1.
I need to order the result by the count of the rows on the table client_closed_1 where meeting=1.
I did it for time_closed field but that was easy because there was no condition.
It's a part of a search code so I'll show you all of it.
With this code I manage to order it by meeting - but users who has no rows with meeting=1 isn't pull out from the database and I need them to show even if they doesn't have meetings.
if (project_type($_GET['project']) == 1) {
$table = 'clients_closed_1';
} else {
$table = 'clients_closed_2';
}
$s_query = "SELECT *,COUNT(time_closed) as numc FROM `".$table."` FULL JOIN `users` ON users.ID=user_c WHERE 1=1";
if (isset($_POST['search'])) {
if ($_POST['tm'] == 'da') {
$dd = strtotime($_POST['y']."-".$_POST['m']."-".$_POST['d']);
$s_query = $s_query." && DATE(FROM_UNIXTIME(time_closed)) = DATE(FROM_UNIXTIME(".$dd."))";
}
elseif ($_POST['tm'] == 'mon') {
$s_query = $s_query." && YEAR(FROM_UNIXTIME(time_closed))=".$_POST['y']." && MONTH(FROM_UNIXTIME(time_closed))=".$_POST['m'];
}
if (!empty($_POST['search_name'])) {
$s_query = $s_query." && CONCAT(p_name,' ',l_name) LIKE '%".$_POST['search_name']."%'";
}
if (!empty($_POST['level'])) {
$query = "&& (level=3 && project IN (SELECT `project` FROM `project` WHERE type='2')) || level=4";
}
} else {
$s_query = $s_query." && YEAR(FROM_UNIXTIME(time_closed))=YEAR(NOW()) && MONTH(FROM_UNIXTIME(time_closed))=MONTH(NOW())";
}
if (isset($_GET['order'])) {
if ($_GET['order'] == 'closing') {
$s_query = $s_query." GROUP BY users.ID ORDER BY numc DESC";
}
elseif ($_GET['order'] == 'meeting') {
$s_query = $s_query." && meeting='1' GROUP BY users.ID ORDER BY numd DESC";
}
}
$query = $db->query($s_query);
If you need any more code/doedn't understand something comment please and I'll fix it.
Thank you.
EDIT:
example of $s_query:
SELECT *,COUNT(time_closed) as numc, COUNT(meeting) as numd FROM `clients_closed_1`
FULL JOIN `users` ON users.ID=user_c WHERE 1=1 &&
YEAR(FROM_UNIXTIME(time_closed))=YEAR(NOW()) &&
MONTH(FROM_UNIXTIME(time_closed))=MONTH(NOW())
GROUP BY users.ID ORDER BY numc DESC
Im not sure I understand 100% of the criteria youre looking for but here is a rough draft of the query:
SELECT c.id, c.meeting, temp1.time_closed_count, temp2.meeting_count, temp3.status_count
FROM `clients_closed_1` c
FULL JOIN `users` u
ON c.user_c=u.ID
LEFT JOIN (SELECT time_closed, count(time_closed) time_closed_count FROM clients_closed_1 GROUP BY time_closed) temp1
ON c.time_closed = temp1.time_closed
LEFT JOIN (SELECT meeting, count(meeting) meeting_count FROM clients_closed_1 GROUP BY meeting) temp2
ON c.meeting = temp2.meeting
LEFT JOIN (SELECT status, count(status) status_count FROM clients_closed_1 GROUP BY status) temp3
ON c.status = temp3.status
WHERE 1=1
AND YEAR(FROM_UNIXTIME(time_closed))=YEAR(NOW())
AND MONTH(FROM_UNIXTIME(time_closed))=MONTH(NOW())
ORDER BY {$order_criteria} DESC
Whats happeneing here is, we are doing the count of all distinct meeting values in a subquery and joining it to the original query based on the value of "meeting" for each row.
This gives us the total "meetings" grouped by distinct meeting values, without cutting out rows. Such is the same for the other 2 subqueries.
This also cleans things up a bit and allows us to just insert the $order_criteria, where that could be time_closed count, meeting_count, or status_count. Just set a default (id) in case your user does not choose anything :)
Edit: Id also recommend trying to get out of the SELECT * habit. Specify the columns you need and your output will be much nicer. Its also far more efficient when you start dealing with larger tables.
After I wrote a really long query to do this I found the perfect soulution.
SELECT SUM(IF(meeting='1' && user_c=users.ID, 1,0)) as meeting_count FROM clients_closed_1 JOIN users
This query return as meeting_count the number of meeting which their value is '1'.
I didn't know I can do such thing until now, so I shared it here. I guess it can be helpull in the future.
Related
i have two table like registration table and academicdetails table and i have two records in academic table according to registration id but i want to join with latest record of academic table.
Query:
SELECT a.*, b.status AS ac_status
FROM registration AS a
LEFT JOIN academicdetails AS b ON a.id = b.registration_id
WHERE b.status = '1'
ORDER BY id DESC
LIMIT 100
PHP code:
if(isset($search['category']) && $search['category']!=''){
$condition = ', TIMESTAMPDIFF(YEAR, a.dob, CURDATE()) AS age';
}
$this->db->select('a.*,b.status as ac_status'.$condition);
$this->db->from("registration as a");
$this->db->join('academicdetails as b','a.id = b.registration_id','left');
if(isset($search['search_by_name']) && $search['search_by_name']!=''){
$this->db->where('a.name', $search['search_by_name']);
}
if(isset($search['gender']) && $search['gender']!=''){
$this->db->where('a.gender', $search['gender']);
}
if(isset($search['status']) && $search['status']!=''){
$this->db->where('b.status', $search['status']);
}
if(isset($search['category']) && $search['category']!=''){
if($search['category']=='Youth'){
$this->db->where('age>10 AND age<18');
}elseif($search['category']=='Junior'){
$this->db->where('age>17 AND age<22');
}elseif($search['category']=='Senior'){
$this->db->where('age > 21');
}
}
if(isset($search['course']) && $search['course']!=''){
$this->db->where('b.course', $search['course']);
}
$this->db->limit($search['limit'], $search['start']);
$this->db->order_by($this->id, $this->order);
$this->db->group_by('a.id');
$query = $this->db->get();
Please try following query:
SELECT * FROM `registration` LEFT JOIN academicdetails on registration.id = academicdetails.id ORDER BY academicdetails.id desc LIMIT 100;
Try this:-
$this->db->select("a.*, b.status AS ac_status");
$this->db->from('registration AS a');
$this->db->join('academicdetails AS b', 'b.registration_id = a.id', 'left');
$this->db->where("b.status",1);
$this->db->order_by("b.id","DESC");//primary key or id of your academic table.
$this->db->limit(100);
$this->db->get();
I have 2 tables - users and articles.
users:
user_id (int)
name (varchar)
articles:
article_id (int)
user_id (int)
title (varchar)
description (text)
In my application I need to display 20 RANDOM articles on a page.
My query is like this:
SELECT a.title
, a.description
, u.name
FROM articles a
JOIN users u
USING (user_id)
ORDER
BY RAND()
LIMIT 20
A user can have any number of articles in the database.
Now the problem is sometimes out of 20 results, there are like 9-10 articles from one single user.
I want those 20 records on the page to not contain more than 3 (or say 4) articles from a particular user.
Can I achieve this through SQL query. I am using PHP and MySQL.
Thanks for your help.
You could try this?
SELECT * FROM
(
SELECT B.* FROM
(
SELECT A.*, ROW_NUMBER() OVER (PARTITION BY A.USER_ID ORDER BY A.R) USER_ROW_NUMBER
FROM
(
SELECT a.title, a.description, u.name, RND() r FROM articles a
INNER JOIN users u USING (user_id)
) A
) B
WHERE B.USER_ROW_NUMBER<=4
) C
ORDER BY RAND() LIMIT 20
Mmm, intresting I don't think this is possible through a pure sql query.
My best idea would be to have an array of the articles that you'll eventually display query the database and use the standard SELECT * FROM Articles ORDER BY RAND() LIMIT 20
The go through them, making sure that you have indeed got 20 articles and no one has breached the rules of 3/4 per user.
Have another array of users to exclude, perhaps using their user id as an index and value of a count.
As you go through add them to your final array, if you find any user that hits you rule add them to the array.
Keep running the random query, excluding users and articles until you hit your desired amount.
Let me try some code (it's been a while since I did php)
$finalArray = [];
$userArray = [];
while(count($finalArray) < 20) {
$query = "SELECT * FROM Articles ";
if(count($finalArray) > 0) {
$query = $query . " WHERE articleID NOT IN(".$finalArray.")";
$query = $query . " AND userID NOT IN (".$userArray.filter(>4).")";
}
$query = $query . " ORDER BY Rand()";
$result = mysql_query($query);
foreach($row = mysql_fetch_array($result)) {
if(in_array($finalArray,$row) == false) {
$finalArray[] = $row;
}
if(in_array($userArray,$row[userId]) == false) {
$userArray[$row[userId]] = 1;
}
else {
$userArray[$row[userId]] = $userArray[$row[userId]] + 1;
}
}
I have two mysql tables memes and votes. Sample data is give below
Memes table
Votes table
vote = 1 is for upvote and vote = 0 is for downvote.
I want result something like this
To calculate up and down votes for each record.
Here is my code only for get records from memes table.
function loadAllMemes($categoryID,$sortby, $page,$perpage)
{
$data = array();
$this->db->select("*");
$this->db->from("memes");
$this->db->limit($perpage, $page);
if($categoryID > 0)
{
$this->db->where("categoryid",$categoryID);
}
if($sortby != "")
{
if($sortby == "recently")
{
$this->db->order_by("date","DESC");
}
else if($sortby == "popular")
{
$this->db->order_by("views","DESC");
}
}
$query = $this->db->get();
return $query->result();
}
Any help will be appreciated. Thanks
select Memes.*, sum(vote) upvote, sum(if(vote = 0, 1, 0)) downvote from memes
join vote on memes.id = memeid
group by memeid
This is not tried out. So kndly excuse if there is any syntax error
NOTE : This is purely in sql. Please do necessary things to convert for codeigneter
For Mysql you can directly put your condition us sum function and this will result a boolean 0/1 and you can use it your query as below
SELECT
m.*,
SUM(v.vote = 1) upvotes, /* this equal to SUM(CASE WHEN v.vote = 1 THEN 1 ELSE 0 END) */
SUM(v.vote = 0) downvote
FROM memes m
JOIN vote v ON m.id = v.memeid
GROUP BY m.id
For active record you can write above query as
$this->db->select('m.*,SUM(v.vote = 1) upvotes,SUM(v.vote = 0) downvote',FALSE)
->from('memes m')
->join('vote v','m.id = v.memeid ')
->group_by('m.id')
->get()
->result();
Quick solution would be by using subquery, although, I think join would be more efficient here.
SELECT
m.id,
m.title,
m.path,
m.date,
m.categoryid,
m.views,
(SELECT COUNT(*) FROM votes WHERE vote = '1' AND memeid = m.id) upvotes,
(SELECT COUNT(*) FROM votes WHERE vote = '0' AND memeid = m.id) downvotes
FROM memes m
What I am trying to do is to get all of the users with the right conditions
so I'm building with a foreach statment a sub_queries to make it work.
the problem is that I got 100,000+ records in the Database.
and this kind of query takes forever to run.
I know I'm not doing it in the best way but I also tried left joins, which was really slow too..
this is the function I'm using:
public function get_affected_users_by_conditions($conditions, $mobile_type)
{
// Basic Query
// Selecting all of the users from `enswitch_mobile users` table
// The total and the `users` with the conditions
$sql = "SELECT COUNT(*) AS `users`,
(SELECT COUNT(*) FROM `enswitch_mobile_users`) AS `total`
FROM
`enswitch_mobile_users` AS `musers`
WHERE
`musers`.`phone_type` = :mobile_type";
$value_counter = 0;
$values = array();
// This is the foreach loop I was talking about
// I am looping all the conditons.
// and when theres a value i'm adding it as a subquery.
foreach($conditions as $cnd) {
switch ($cnd['condition']) {
// REALLY SLOW SUB-QUERY:
case 'talked_atleast':
$value_counter++;
// Here I'm trying to CUT the query by users who talked atleast $value seconds
$sql .= " AND (SELECT SUM(TIME_TO_SEC(TIMEDIFF(`finished_call`,`start_call`))) FROM `enswitch_calls` WHERE `user_id` = `musers`.`id`) >= :value".$value_counter;
$values[$value_counter] = $cnd['value'];
break;
// REALLY SLOW SUB-QUERY:
case 'purchase_atleast':
// Here I am trying to CUT the users by subquery who check if the users has bought at least $value times
$value_counter++;
$sql .= " AND (SELECT COUNT(*) FROM (SELECT user_id FROM enswitch_new_iphone_purchases
UNION
SELECT user_id FROM enswitch_new_android_purchases) AS p WHERE `status` > 0 AND user_id` = `musers`.`id`) >= :value".$value_counter;
$values[$value_counter] = $cnd['value'];
break;
// REALLY SLOW SUB-QUERY:
case 'never_purchase':
// Here I am trying to CUT the users by subquery to get only the users who never made a puchase.
$sql .= ' AND (SELECT COUNT(*) FROM (SELECT user_id FROM enswitch_new_iphone_purchases
UNION
SELECT user_id FROM enswitch_new_android_purchases) AS p WHERE `status` = 0 AND `user_id` = `musers`.`id`) = 0';
break;
}
}
$query = DB::query(Database::SELECT, $sql);
$query->bind(':mobile_type', $mobile_type);
// Looping the values and binding it into the SQL query!
foreach ($values as $k => $v) {
$query->bind(':value'.$k, $values[$k]);
}
// Executing query
$result = $query->execute();
return array('total_users' =>$result[0]['total'], 'affected_users'=>$result[0]['users']);
}
EDIT:
The Slowest Query as Requested: (MySQL)
SELECT COUNT(*) AS `users`,
( SELECT COUNT(*)
FROM `enswitch_mobile_users`
) AS `total`
FROM `enswitch_mobile_users` AS `musers`
WHERE `musers`.`phone_type` = 'iphone'
AND ( SELECT COUNT(*)
FROM ( SELECT `status`,
`user_id`
FROM `enswitch_new_iphone_purchases`
UNION
SELECT `status`,
`user_id`
FROM `enswitch_new_android_purchases`
) AS `p`
WHERE `p`.`status` > 0
AND `p`.`user_id` = `musers`.`id`
) >= 2
The subquery in the second SELECT column will execute for every m_users row that passes the WHERE condition:
SELECT
COUNT(*) AS users,
(SELECT COUNT(*) FROM enswitch_mobile_users) AS total <-- here's the problem
FROM enswitch_mobile_users AS musers
WHERE musers.phone_type = whatever
If I'm reading this correctly, you need a one-row result with the following columns:
users - number of enswitch_mobile_users rows with the specified phone_type
total - count of all enswitch_mobile_users rows
You can get the same result with this query:
SELECT
COUNT(CASE WHEN musers.phone_type = whatever THEN 1 END) AS users,
COUNT(*) AS total
FROM enswitch_mobile_users
The CASE checks for the phone type, and if it matches the one you're interested it it yields a 1, which is counted. If it doesn't match, it yields a NULL, which is not counted.
I added the line AND t.cover='$c' and now it will not show any results. However, all variables are correct. If I run the line in phpMyAdmin it will display the results fine. Am I missing syntax in PHP? If I remove the t.cover='$c' the script works fine on the web. $c is simply a $_GET['c'] that will either be empty or will equal 0, which I redefine to 'No'.
if($t != '') {
if($c == 'No') {
$getListings = mysql_query("SELECT * FROM events t LEFT JOIN bars b on t.venue=b.name WHERE t.date='$date' AND t.type='$t' AND t.cover='$c' AND b.active='1' ORDER BY t.promoted DESC, t.order ASC");
} else {
$getListings = mysql_query("SELECT * FROM events t LEFT JOIN bars b on t.venue=b.name WHERE t.date='$date' AND t.type='$t' AND b.active='1' ORDER BY t.promoted DESC, t.order ASC");
}
} else {
if($c == 'No') {
$getListings = mysql_query("SELECT * FROM events t LEFT JOIN bars b on t.venue=b.name WHERE t.date='$date' AND t.cover='$c' AND t.type='$t' AND b.active='1' ORDER BY t.promoted DESC, t.order ASC");
} else {
$getListings = mysql_query("SELECT * FROM events t LEFT JOIN bars b on t.venue=b.name WHERE t.date='$date' AND b.active='1' ORDER BY t.promoted DESC, t.order ASC");
}
}
My biggest confusion is why it works in phpMyAdmin but yet displays no rows on the web.
It seems I have tried everything on Google and still no luck. I wish it would throw an error or something to point me in a direction, but it just comes up as though the query was empty. If anyone has any idea, I am still looking for input. Maybe LEFT JOIN is doing it?
...LEFT JOIN bars b on t.venue=b.name AND b.active=1 ...
Remove the b.active from the WHERE otherwise it will be a condition for the SELECT
The current queries have SELECT... WHERE ... b.active = 1 ... meaning it can only return results where that condition is met