Convert mysql sub query to Laravel - mysql

How I can convert mysql query to laravel query? this is the query
SELECT
users.first_name,
users.id AS uid,
(
SELECT
COUNT(vpl_submissions.accept)
FROM
vpl_submissions
INNER JOIN vpl ON vpl_submissions.vpl = vpl.id
WHERE vpl.courseid = 2
AND vpl_submissions.accept = 1
AND vpl_submissions.userid = uid
) AS completed
FROM
users
INNER JOIN course_enroles ON users.id = course_enroles.user_id
WHERE course_enroles.course_id = 2

You can try this.
DB::table('users')
->join('course_enroles', 'users.id', '=', 'course_enroles.user_id')
->where("course_enroles.course_id", "=", 2)
->select(users.first_name,users.id AS uid,
DB::raw("(SELECT COUNT(vpl_submissions.accept) FROM
vpl_submissions INNER JOIN vpl ON vpl_submissions.vpl = vpl.id
WHERE vpl.courseid = 2
AND vpl_submissions.accept = 1
AND vpl_submissions.userid = uid) as completed")
)
->get();
This will help you.

Until you find a better solution, you can run raw queries like this
$result = DB::select(DB::raw("
select users.first_name,users.id AS uid,
(
select count(vpl_submissions.accept)
FROM vpl_submissions
INNER JOIN vpl on vpl_submissions.vpl = vpl.id
WHERE vpl.courseid=2 AND vpl_submissions.accept =1
AND vpl_submissions.userid = uid
) as completed
from users
inner join course_enroles on users.id = course_enroles.user_id
where course_enroles.course_id = 2
"));

Related

Need help converting sql query into laravel query

I need the following sql query into laravel query or how can I run into laravel as it is. thanks
SELECT t.*
FROM (SELECT client_id,product_name, max(real_date) AS max_date
FROM api_hilbertis.transactions
WHERE active = 1 AND real_date <= '2019-03-01'
GROUP BY client_id, product_name) AS m
INNER JOIN api_hilbertis.transactions AS t
ON t.client_id = m.client_id
AND t.product_name = m.product_name
AND t.real_date = m.max_date
AND active = 1
ORDER BY real_date DESC;
You can use laravel raw queries.
Example HERE
So for your case it we could write something like:
$results = DB::select( DB::raw("
SELECT t.*
FROM (SELECT client_id,product_name, max(real_date) AS max_date
FROM api_hilbertis.transactions
WHERE active = 1 AND real_date <= '2019-03-01'
GROUP BY client_id, product_name) AS m
INNER JOIN api_hilbertis.transactions AS t
ON t.client_id = m.client_id
AND t.product_name = m.product_name
AND t.real_date = m.max_date
AND active = 1
ORDER BY real_date DESC"
));
EDIT: example with variables:
$someVariable = $request->input('some_variable');
$results = DB::select( DB::raw("SELECT * FROM some_table WHERE some_col = :somevariable"),
['somevariable' => $someVariable,]
);

Mysql how can i sql two tables and count groupid from the second

I'm trying to make an Sql , that get all data from first table "posts_main" and then get the count of comments from second table "posts_comments"
I tried:
$sql = "SELECT * FROM posts_main, count(posts_comments.groupid)
INNER JOIN posts_comments ON posts_comments.groupid = posts_main.id
WHERE posts_main.user_id = '$user_id'
GROUP BY posts_main.id";
Please, how can i do that ?
thanks....
Try with below query.
$sql = "select posts_main.*,
(select groupid from posts_comments where groupid = posts_main.id group by groupid ) as count_group
from posts_main
WHERE posts_main.user_id = '$user_id' ";
Try this with subquery
SELECT posts.*,cnt FROM posts_main
INNER JOIN (select posts_comments.groupid,count(posts_comments.groupid) as cnt
group by posts_comments.groupid)a
ON a.groupid = posts_main.id
WHERE posts_main.user_id = '$user_id'

proper indexing for fast retrieval in mysql

SELECT A.*
FROM
(SELECT *
FROM
(SELECT os.connectioninfoid,
os.schemaname,
con.name AS connectionname,
wiki.wikitext,
ot.oeschemaid,
ot.tablename,
ot.tabledescription,
ot.rowcount,
oc.*
FROM oecolumn oc
JOIN oetable ot ON (oc.oetableid = ot.oetableid)
JOIN oeschema os ON os.oeschemaid = ot.oeschemaid
JOIN connectioninfo con ON os.connectioninfoid = con.connectioninfoid
JOIN acl_object_identity oi ON oi.object_id_identity = ot.oetableid
JOIN acl_class CLASS ON (oi.object_id_class = class.id)
JOIN acl_entry entry ON (entry.acl_object_identity = oi.id)
JOIN acl_sid sid ON (entry.sid = sid.id)
JOIN ROLES ROLE ON (sid.sid = role.authority)
JOIN user_role usr ON (usr.roleid = role.roleid)
LEFT OUTER JOIN wiki ON oc.oecolumnid = wiki.wikiobjectid
AND wikiobject = 'oecolumn'
AND wiki.wikitext LIKE '%'
WHERE ot.type='DB'
AND class.class = 'com.ovaledge.oasis.domain.OeTable'
AND entry.mask = 1
AND usr.userid = 'admin' ) hts
WHERE tablename LIKE '%'
AND columnname LIKE '%'
AND columndescription LIKE '%' )A
I have this query what should be indexed for fast retrieval.

Options to update PDO Query

How i update this query?
I try get all category with more than 1 product, counting this products to show in her side, but my query is too slow (2s). How i do the same, if possible, but more faster?
SELECT
C.id, C.name, C.id_dept, C.cat_type, C.num_prod
FROM
(SELECT
C.id, C.name, C.id_dept, C.cat_type,
COALESCE((SELECT count(P.id) FROM Products P WHERE P.status = 1 AND P.promo = 1 AND P.id_cat = C.id ), 0) AS num_prod
FROM
Products_Cat C
) C
WHERE
C.num_prod > 0 AND C.cat_type = 1 AND C.id_dept = '{IDD}'
ORDER BY C.name ASC
Update PHP PDO
$sql = $db->prepare("UPDATE `Products` set `name` = :name, `id_dept` = :id_dept, `cat_type` = :cat_type WHERE status = 1 AND promo = 1);
//bind all parameters
$sql->bindParam(':name',$name);
.
.
.
$sql->execute();

Duplicate column name 'user_id' problem

I'm having a problem trying to count the number of user records according to the user's id, however I'm using a subquery to join 2 tables that one has a count parameter but I get an error saying duplicate column name 'user_id.
The query:
$sql = "SELECT loc.location_id,
COUNT(loc.location_id) AS total_records
FROM locations loc
LEFT JOIN
(
SELECT usr.*,
loc.*
FROM
(
members usr
INNER JOIN locations loc
)
WHERE usr.user_id = " . $user_id . "
AND usr.account_disabled = 0
ORDER BY loc.submit_date DESC
) usr ON (loc.user_id = usr.user_id)";
All I need it is to return the user's info and the total_records count done by the COUNT function.
Cheers.
EDIT:
This is what I get returned for this SQL:
SELECT loc.location_id,
loc.street_name,
loc.city,
loc.state,
loc.county,
loc.country,
usr.user_id,
usr.username,
COUNT(loc.location_id) AS total_records
FROM locations loc
INNER JOIN members usr ON (loc.user_id = usr.user_id)
WHERE loc.user_id = $user_id
AND usr.account_disabled = 0
GROUP BY loc.location_id
It's not exactly clear why you've got the derived resultset or the LEFT JOIN. Try this:
SELECT loc.location_id,
COUNT(loc.location_id) AS total_records
FROM LOCATIONS_TABLE loc
INNER JOIN MEMBERS_TABLE usr
ON (loc.user_id = usr.user_id)
WHERE loc.user_id = $user_id
AND usr.account_disabled = 0
GROUP BY loc.location_id
I think the problem is this part:
SELECT usr.*,
loc.*
Both tables have a user_id