How to write Subquery in Select statement laravel 5.1? - php

How to write this query in laravel 5.1
SELECT
(SELECT firstname FROM registration_details a INNER JOIN users b
ON a.id = b.registerid
WHERE c.patientid = b.id ) AS patient,
(SELECT firstname FROM registration_details a INNER JOIN users b
ON a.id = b.registerid
WHERE c.doctorid = b.id ) AS doctor,
c.`appoinmentdate`
FROM `appoinments` c
I tried like this But i get Undefined property: stdClass::$id error
DB::table('appoinments')
->select(DB::raw(
"(SELECT firstname FROM registration_details INNER JOIN users ON registration_details.id = users.registerid WHERE appoinments.patientid = users.id) AS patient",
"(SELECT firstname FROM registration_details INNER JOIN users ON registration_details.id = users.registerid WHERE appoinments.doctorid = users.id) As doctor",
"appoinments.appoinmentdate",
"appoinments.id",
"(SELECT timings FROM appoinment_time WHERE appoinment_time.id = appoinments.appoinment_time) AS apptime ",
"(SELECT branchname FROM branches WHERE branches.id = appoinments.branchcode) AS branch"))
->get();

Finally got the answer. Thanks to all who try for me.
$appoinment = DB::table('appoinments')->select(DB::raw("(SELECT firstname FROM registration_details INNER JOIN users ON registration_details.id = users.registerid WHERE appoinments.patientid = users.id) AS patientid"));
$appoinment = $appoinment->addSelect(DB::raw("(SELECT firstname FROM registration_details INNER JOIN users ON registration_details.id = users.registerid WHERE appoinments.doctorid = users.id) As doctorid"));
$appoinment = $appoinment->addSelect(DB::raw("appoinments.appoinmentdate"));
$appoinmentlist = $appoinment3->addSelect(DB::raw("appoinments.id"))->get();

Related

how to write a left join for make loop

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)

Why is my member id column returning null?

For some reason the member id field(auto inc.) in my huge query is returning null.I've tried every which way of selecting it... m.member_id AS member_id, etc.I cannot figure out why it is returning null when there is a value for that field in the table.
<?php
public function get_info($criteria = 0){
if(is_numeric($criteria)){
$where = "WHERE m.member_id = ".$criteria;
} else {
$where = "WHERE email_address = '".$criteria."'";
}
$query_member = "
SELECT
m.member_id AS member_id, m.display_name, m.email_address, m.group_id, m.status, m.activation_code, UNIX_TIMESTAMP(m.date_joined) AS date_joined,
m.gender, m.location, m.biography, m.mantra, m.birth_date, m.results_per_page, m.admin_emails, m.member_emails, m.last_active, m.avatar_id,
m.banner_id, m.signature, m.newsletter_subscription, m.recruiting_status, m.facebook_username, m.website, m.steam_username, m.xboxlive_gamertag, m.psn_id,
g.group_id, g.title, g.description,
a.attachment_id, a.file_name,
f.message_id, f.author_id, COUNT(f.message_id) AS forum_count,
b.attachment_id AS banner_id, b.file_name AS banner_file,
mr.request_id, mr.author_id, mr.recipient_id, mr.status, COUNT(mr.request_id) AS total_friends,
tm.team_member_id, tm.member_id, tm.team_id
FROM members AS m
LEFT JOIN member_groups AS g ON (m.group_id = g.group_id)
LEFT JOIN attachments AS a ON (m.avatar_id = a.attachment_id)
LEFT JOIN forum_messages AS f ON (m.member_id = f.author_id)
LEFT JOIN attachments AS b ON (m.banner_id = b.attachment_id)
LEFT JOIN member_requests AS mr ON (m.member_id = mr.author_id OR m.member_id = mr.recipient_id) AND mr.status = 1
LEFT JOIN team_members AS tm ON (m.member_id = tm.member_id) AND date_left = ''
".$where."
GROUP BY m.member_id
LIMIT 1";
//show_error($query_member);
if($query_member = $this->db->query($query_member)){
if($query_member->num_rows() > 0){
var_dump($query_member->row_array());
Because you select two fields with the same name. So MySQL will return result of last one. Add aliases:
SELECT m.member_id AS member_id_1, tm.member_id AS member_id_2 ...

Sql Query Retrieving Data from Multiple Tables

i want to retrieve Data form 3 specifies Tables namely
UserDetail(Fname,Lname,User_id),
Movies(Movie_id,MovieName)
UserLikedMovies(User_id,Movie_id)
such that when a user enter a specific Movie_id then Userid Fname Lname form User detail MovieName from Movies,,
Here is what i tried
SELECT UserDetail.FName
FROM
UserDetail UserDetail
INNER JOIN
UserLikedMovies UserLikedMovies
ON
UserDetail.User_id = UserLikedMovies.User_id
INNER JOIN
(
SELECT
Movies.MovieName,
Movies.Movie_id
FROM
Movies Movies
INNER JOIN
UserLikedMovies UserLikedMovies
ON
Movies.Movie_id = UserLikedMovies.Movie_id
INNER JOIN
UserDetail UserDetail
ON
UserLikedMovies.User_id = UserDetail.User_id
WHERE
Movies.Movie_id IN ( Select UserLikedMovies.Movie_id from UserLikedMovies where UserLikedMovies.Movie_id = 4)
) as ABC
ON UserLikedMovies.Movie_id = ABC.Movie_id
AND Movies.Movie_id = ABC.Movie_id
Suppose movie_id is 9, then query is below.
SELECT User.Fname, User.Lname, (SELECT MovieName FROM Movies WHERE Movie_id = 9) AS MovieName
FROM UserDetail User
INNER JOIN UserLikedMovies Like
ON User.User_id = Like.User_id
WHERE Like.Movie_id = 9;
It is pretty simple query:
SELECT ud.`User_id`, ud.`Fname`, ud.`Lname`, m.`MovieName`
FROM `Movies` m
RIGHT JOIN `UserLikedMovies` ulm ON ulm.`Movie_id` = m.`Movie_id`
LEFT JOIN `UserDetail` ud ON ud.`User_id` = ulm.`User_id`
WHERE m.`Movie_id` = 4
I do not know why you are using fearful sub-queries for this simple task.

insert php code in sql query

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;
}

SQL + Ion Auth + CodeIgniter?

I have the following SQL:
SELECT (
(SELECT SUM(vote_score)
FROM question_votes
JOIN questions
ON vote_question = q_id
JOIN users
ON q_author = users.id
WHERE q_author` = users.id)
+
(SELECT SUM(vote_score)
FROM answer_votes
JOIN answers
ON vote_answer = a_id
JOIN users
ON a_author = users.id
WHERE a_author = users.id)) AS rep
and I want it to be added here (Ion Auth's method):
$this->db->select(
array(
$this->tables['users'].'.*',
$this->tables['groups'].'.name AS '. $this->db->protect_identifiers('group'),
$this->tables['groups'].'.description AS '. $this->db->protect_identifiers('group_description'),
"(SELECT COUNT(`a_author`) FROM `answers` WHERE a_author = users.id) + (SELECT COUNT(`q_author`) FROM `questions` WHERE q_author = users.id ) AS total_posts",
"SELECT
((SELECT SUM(vote_score)
FROM question_votes
JOIN questions
ON vote_question = q_id
JOIN users
ON q_author = users.id
WHERE q_author` = users.id)
+
(SELECT SUM(vote_score)
FROM answer_votes
JOIN answers
ON vote_answer = a_id
JOIN users
ON a_author = users.id
WHERE a_author = users.id)) AS rep"
)
);
but I get the followin error:
Did you read the error message?
You have a (`) where shouldn't be any.
Change this: WHERE q_author' = users.id for this: WHERE q_author = users.id

Categories