Options to update PDO Query - mysql

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();

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,]
);

OR condition with Joins using cakephp and mysql

Iam searching a keyword in solr and it returns matched resume id's,when I got the result from solr I am searching those documents and getting matched job seekers from my database.
Now my question is have to get the record if it match in documents or in job seekers skills i.e job_seekers.skills column how to write the query..
Here is my existing query..
SELECT *
FROM `job_seekers` AS `JobSeeker`
LEFT JOIN `job_seeker_documents` AS `JobSeekerDocument` ON (`JobSeeker`.`id` = `JobSeekerDocument`.`job_seeker_id` AND `doc_attachment` IN ('1457448773Jan.doc', '1457448764Eric.doc', 'Vijal_Chokshi_Profile.doc', 'Deborah_Project manager_Profile..docx'))
LEFT JOIN `config_work_authorizations` AS `ConfigWorkAuthorization` ON (`JobSeeker`.`work_authorization` = `ConfigWorkAuthorization`.`id`)
LEFT JOIN `employees` AS `Employee` ON (`Employee`.`job_seeker_id` = `JobSeeker`.`id`)
WHERE `JobSeeker`.`company_id` = 11 AND `JobSeeker`.`skills` = 'java' AND `JobSeeker`.`bench_status` IN (0, 1) AND (select count(*) from employee_jobs where `employee_jobs`.`employee_id`=`Employee`.`id` and `employee_jobs`.`job_status` =1 ) = 0 GROUP BY `JobSeeker`.`id` HAVING (COUNT(`JobSeekerDocument`.`id`) > 0 );
you can write or condition in brackets i.e. () like
(`JobSeeker`.`skills` = 'java' OR `JobSeekerDocument`.`skills` = 'java')
In your query
SELECT *
FROM `job_seekers` AS `JobSeeker`
LEFT JOIN `job_seeker_documents` AS `JobSeekerDocument` ON (`JobSeeker`.`id` = `JobSeekerDocument`.`job_seeker_id` AND `doc_attachment` IN ('1457448773Jan.doc', '1457448764Eric.doc', 'Vijal_Chokshi_Profile.doc', 'Deborah_Project manager_Profile..docx'))
LEFT JOIN `config_work_authorizations` AS `ConfigWorkAuthorization` ON (`JobSeeker`.`work_authorization` = `ConfigWorkAuthorization`.`id`)
LEFT JOIN `employees` AS `Employee` ON (`Employee`.`job_seeker_id` = `JobSeeker`.`id`)
WHERE `JobSeeker`.`company_id` = 11
AND (`JobSeeker`.`skills` = 'java' OR `JobSeekerDocument`.`skills` = 'java')
AND `JobSeeker`.`bench_status` IN (0, 1)
AND (select count(*) from employee_jobs where `employee_jobs`.`employee_id`=`Employee`.`id` and `employee_jobs`.`job_status` =1 ) = 0
GROUP BY `JobSeeker`.`id`
HAVING (COUNT(`JobSeekerDocument`.`id`) > 0 );

MySQL - combining multiple queries

I currently have the following queries (and some surrounding vB code). I was hoping I could slim this up into a single SELECT statement versus having to run 10 more on each page to get that names of individuals.
$results = $db->query_read_slave("
SELECT user.id AS steam, user.skills AS level
FROM wcsp.wcgousers AS user
WHERE race = '_wcs_'
ORDER BY ABS(user.skills) DESC LIMIT 10
");
$rank = 1;
while ($user = $db->fetch_array($results)) {
$result = $db->query_first("
SELECT old.name AS name
FROM wcsp.warn_oldnames AS old
WHERE id = '$user[steam]'
ORDER BY lasttime
DESC LIMIT 1
");
$listing[] = array("id" => $user['steam'], "level" => $user['level'], "name" => $result['name'], "rank" => $rank);
$rank += 1;
}
I have tried LEFT JOIN but the issue I run into is that I would need a subquery in the LEFT JOIN similar to:
SELECT user.id AS steam, user.skills AS level, names.name AS name
FROM wcsp.wcgousers AS users
LEFT JOIN
(
SELECT names.name
FROM wcsp.warn_oldnames AS names
WHERE id = wcsp.wcgousers.id
ORDER BY lasttime DESC LIMIT 1
) AS names
ON names.id = users.id
WHERE users.race = '_wcs_'
Which won't work due to the different database check inside the subquery.
If I understand you correctly, you want to get the latest Name for every users.
SELECT a.id AS steam,
a.skills AS level,
b.name
FROM wcgousers a
INNER JOIN warn_oldnames b
ON a.ID = b.ID
INNER JOIN
(
SELECT ID, MAX(lasttime) max_date
FROM warn_oldnames
GROUP BY ID
) c ON b.ID = c.ID AND
b.lastTime = c.max_date
WHERE a.race = '_wcs_'
-- ORDER BY ABS(a.skills) DESC
-- LIMIT 10

Complicated MySQL Query Problem

I have a problem with a MySQL Query:
I have two tables:
- clustercategories
- domains
Now I have a SQL Query which lists all Domains of a specific category with category name - this is my Query:
SELECT domains.*, clustercategories.clustercategoryname
FROM (domains, clustercategories)
WHERE ((clustercategories.id = 3 AND (domains.cluster1id = 3 OR domains.cluster2id = 3))
OR (clustercategories.id = 10 AND (domains.cluster1id = 10 OR domains.cluster2id = 10)))
AND domains.status = '1'
GROUP BY domains.name
ORDER BY domains.name
The Problem is now, that I also have a third table "subpages" where I want to count all entries of a specific domain with status = '1' and I don't know how to modify my query to work - I have tried this query, but I do not get any results:
SELECT domains.*, clustercategories.clustercategoryname
FROM (domains, clustercategories)
WHERE ((clustercategories.id = 3 AND (domains.cluster1id = 3 OR domains.cluster2id = 3) AND (SELECT COUNT(*) AS total FROM subpages WHERE subpages.domainid = domains.id AND subpages.status = '1'))
OR (clustercategories.id = 10 AND (domains.cluster1id = 10 OR domains.cluster2id = 10) AND (SELECT COUNT(*) AS total FROM subpages WHERE subpages.domainid = domains.id AND subpages.status = '1')))
AND domains.status = '1'
GROUP BY domains.name
ORDER BY domains.name
Has anyone any ideas?
I think that you'll want to put your subquery into your projection, like this:
SELECT domains.*, clustercategories.clustercategoryname,
(SELECT COUNT(*) FROM subpages WHERE subpages.domainid = domains.id AND subpages.status = '1') AS total
FROM domains, clustercategories
WHERE ((clustercategories.id = 3 AND (domains.cluster1id = 3 OR domains.cluster2id = 3))
OR (clustercategories.id = 10 AND (domains.cluster1id = 10 OR domains.cluster2id = 10)))
AND domains.status = '1'
GROUP BY domains.name
ORDER BY domains.name
It looks to me your first query can be rewritten like this
SELECT d.*
, cc.clustercategoryname
FROM domains d
INNER JOIN clustercategories cc
ON cc.id = d.cluster1id
OR cc.id = d.cluster2id
WHERE cc.id IN (3, 10)
AND d.status = '1'
GROUP BY
d.name
ORDER BY
d.name
thus adding the count of subpages can be done like this
SELECT d.*
, cc.clustercategoryname
, sp.total
FROM domains d
INNER JOIN clustercategories cc
ON cc.id = d.cluster1id
OR cc.id = d.cluster2id
LEFT OUTER JOIN (
SELECT COUNT(*) AS total
, domainid
FROM subpages
WHERE subpages.status = '1'
GROUP BY
domainid
) sp ON sp.domainid = d.domainid
WHERE cc.id IN (3, 10)
AND d.status = '1'
GROUP BY
d.name
ORDER BY
d.name

mysql syntax how to add a third table to $query

I have code:
$query = "SELECT a.*, c.name as categoryname, c.id as categoryid
FROM #__table_one as a
LEFT JOIN #__table_two c ON c.id = a.catid";
$query .= " WHERE a.published = 1
AND a.access <= {$aid}
AND a.trash = 0
AND c.published =
AND c.access <= {$aid}
AND c.trash = 0";
I would like to add a third table ('__some_table') for the parts of the query where a.publish, a.access and a.trash. In other words, I want these fields to be retrieved from another table, not "#__table_one", but I do not know how to incorporate the #__some_table into the current query
I imagine the JOIN command can help me, but I do not know how to code mysql
//not tested
$query = "SELECT a.*, c.name as categoryname, c.id as categoryid
FROM #__table_one as a
LEFT JOIN #__table_two c ON c.id = a.catid
LEFT JOIN #__table_three d ON d.id = a.some_id";