I want to write this code in one query with left join statement. Currently I use php for loop this query but its not good and is too slow
It's app that have like and dislike option. I want number of every user's like
public function getUserLikedCount($id) {
$query = $this->db->query(
"SELECT * FROM `users`, post, `like`
WHERE users.id = post.post_user_id
AND like.like_post_id = post.post_id
AND users.id = ?
AND post.post_is_active = 1", array($id)
);
return $query->num_rows();
}
thanks
Try like it ( I don't know your table like structure):
$sql = 'SELECT u.id AS `user_id`
COUNT(p.post_id) AS `liked_cnt` <-- or like.id ( if this field exists)
FROM `post` AS p
LEFT JOIN `users` AS u
ON ( u.id = p.post_user_id )
LEFT JOIN `like` AS l
ON ( l.like_post_id = p.post_id )
WHERE p.post_is_active = 1
AND l.status = 1 <-- "liked"
GROUP BY u.id ';
Try this :
$query = $this->db->query(
"SELECT * FROM `users` u
LEFT JOIN post p ON u.id = p.post_user_id AND u.id =?
LEFT JOIN like l ON l.like_post_id = p.post_id AND p.post_is_active = 1
", array($id)
Related
I'm trying to select an specific information from a table that depends on the conditions of two other tables. When I try separate conditions work, but when I try to work with two conditions does not.
With this I get the correct data from table:
$sql = "SELECT * FROM commerce_order WHERE m_key = 'total'";
And with this I also get the result from table:
$sql = "SELECT *
FROM commerce_order M
JOIN commerce_order_items I
ON M.item_id = I.item_id
JOIN produtcs P
ON I.order_id = P.ID
WHERE status = 'complete'";
but when I try this I don't get any result:
$sql = "SELECT *
FROM commerce_order M
WHERE m_key = 'total'
JOIN commerce_order_items I
ON M.item_id = I.item_id
JOIN produtcs P
ON I.order_id = P.ID
WHERE status = 'complete'";`
I expected the first code result to be filtered by the second code condition. I know which are the values from the first table that should return, but I don't get any.
You can't put WHERE m_key = 'total' before JOIN all your WHERE clauses should go after all your JOINs:
$sql = "SELECT *
FROM commerce_order M
JOIN commerce_order_items I
ON M.item_id = I.item_id
JOIN produtcs P
ON I.order_id = P.ID
WHERE status = 'complete' AND m_key = 'total'";
Trying to query user comments by specific user role. The idea is to display comments by user group/user role only. The code below shows all existing user comments but doesn't filter by user roles.
$query = $wpdb->prepare( "SELECT *
FROM $wpdb->comments c
LEFT JOIN $wpdb->commentmeta cm ON c.comment_ID = cm.comment_id
LEFT JOIN $wpdb->users u ON c.user_id = u.ID
WHERE c.comment_post_ID = %s
AND c.comment_approved = 1
ORDER BY c.comment_ID $comment_order
",$post->ID);
$comments = $wpdb->get_results($query);
I feel like I need to insert something like this into the above query
ON wp_users.ID = wp_usermeta.user_id
WHERE wp_usermeta.meta_key = 'wp_capabilities'
AND wp_usermeta.meta_value LIKE '%".$user_rol->roles[0]."%'
Here is my query worked well but want to add another condition in it but it giving error Not unique table/alias: 'users'
Here is my code
$query = 'select group_concat(DISTINCT evaluation_section. section_name) as allowed_sections from evaluation_section
join section_permissions on (section_permissions.section_id = evaluation_section.section_id )
join users on ( users.user_id = section_permissions.user_id )
join users on ( users.user_id = section_permissions.assigned_for )
where users.user_id = '.$user['user_id'];
want select name from users table also and then use this condition join users on (users.user_id = section_permissions.assigned_for )
please help me
This is my edited text
this is my full working query
$q = 'select group_concat(DISTINCT evaluation_section. section_name) as allowed_sections from evaluation_section
join section_permissions on (section_permissions.section_id = evaluation_section.section_id )
join users on ( users.user_id = section_permissions.user_id )
where users.user_id = '.$user['user_id'];
but i want it to select name from users table and then match it with users.user_id = section_permissions.assigned_for so that it get the name of assigned_for id name
as before group_concat I use users.name then its working but it display the name of users.user_id = section_permissions.user_id but i want to display name of er_id )
where users.user_id = '.$user['user_id'];
but i want it to select name from users table and then match it with users.user_id = section_permissions.assigned_for
in same query because in two its not working
Try doing it this way:
$query = 'select group_concat(DISTINCT evaluation_section. section_name) as allowed_sections from evaluation_section
join section_permissions on (section_permissions.section_id = evaluation_section.section_id )
join users u1 on ( u1.user_id = section_permissions.user_id )
join users u2 on ( u2.user_id = section_permissions.assigned_for )
where u1.user_id = '.$user['user_id'];
Since you are joining "users" table twice, you will have to provide unique alias name to both of them.
I'm new to mysql and I have a problem with selecting data from mysql database:
$post_id = 3;
$current_user_id = 1;
$query = "SELECT `comments`.*, `users`.`username`, `users`.`image` FROM (`comments`)
LEFT JOIN `users` ON `comments`.`user_id` = `users`.`id`
WHERE `comments`.`post_id` = '$post_id'
AND `comments`.`status` = 1
AND `users`.`status` = 1
ORDER BY `comments`.`date` desc";
This select, selects all approved comments from database, but also, in this select, I need all unapproved comments of $current_user_id,
Result must look like:
[all approved post comments] + [all unapproved post comments of $current_user_id]
not sure if this will really work, but just give a try and see. I can't test the query since we don't have the schema.
SELECT `comments`.*, `users`.`username`, `users`.`image` FROM (`comments`)
LEFT JOIN `users` ON `comments`.`user_id` = `users`.`id`
WHERE `comments`.`post_id` = '$post_id'
AND (
(`comments`.`status` = 1 AND `users`.`status` = 1)
OR
( `comments`.`status` = 0 AND `users`.`id`= '$current_user_id' )
)
ORDER BY `comments`.`date` desc
What I thought is select all the approved comments OR ( comments that are not approved, but from this user ). You might have to alter the query till you get what you really needed, I'm just giving you the idea, not the exact query. Hope this will help you.
SELECT `comments`.*, `users`.`username`, `users`.`image` FROM (`comments`)
LEFT JOIN `users` ON `comments`.`user_id` = `users`.`id`
WHERE `comments`.`post_id` = '$post_id'
AND `comments`.`status` = 1
AND `users`.`status` = 1
UNION ALL
SELECT `comments`.*, `users`.`username`, `users`.`image` FROM (`comments`)
LEFT JOIN `users` ON `comments`.`user_id` = `users`.`id`
WHERE `comments`.`post_id` = '$post_id'
AND `comments`.`status` = 0
AND `users`.`id` = '$current_user_id'
ORDER BY `comments`.`date` desc
function search_num_rows($param){
$company_name=$param['company_name'];
$loan_no=$param['loan_no'];
$q = $this->db->query("select Count(0) as num_rows
from contact_new
inner join companies c on contact_new.company_id = c.id
inner join history on contact_new.id = history.receiver_email
inner join escalation_level on contact_new.escalation_level_id = escalation_level.id
inner join departments on contact_new.departmend_id = departments.id
WHERE loan_no= '$loan_no' if($company_name){ AND company_name= '$company_name'} ")->result();
return $q[0]->num_rows;
}
can i insert the php code as i done in where clause.Is there any other way to do this without using active records.
It's actually very easy:
function search_num_rows($param){
$company_name = (isset($param['company_name']) && !empty($param['company_name']) ? " AND company_name = '$param[company_name]'" : '');
$loan_no=$param['loan_no'];
$q = $this->db->query("select Count(0) as num_rows
from contact_new
inner join companies c on contact_new.company_id = c.id
inner join history on contact_new.id = history.receiver_email
inner join escalation_level on contact_new.escalation_level_id = escalation_level.id
inner join departments on contact_new.departmend_id = departments.id
WHERE loan_no= '$loan_no' $company_name")->result();
return $q[0]->num_rows;
}