How can I translate multiple select like this:
select p.idProcesso, p.idParceiro, p.comissao ,p.NProcesso, p.dataPedido , p.nomeCliente,
e.Descricao, p.dataVisita, i.nomeImovel, f.desc, p.file_certificadoProvisorio, p.adenePaga,
(SELECT nome from users as u where u.id = p.idAgencia) as nomeAgencia,
(SELECT nome from users as u where u.id = p.idParceiro) as nomeParceiro
FROM processo as p
LEFT JOIN imovel as i on i.idImovel = p.idImovel
LEFT JOIN familia as f on f.idTipologia = p.idTipologia
LEFT JOIN estadoscertificado as e on e.CodEstadosCertificado = p.idEstado
ORDER BY p.NProcesso desc
into laravel query builder ?
Thank you
Just use the RAW method...
DB::select(DB::raw('select p.idProcesso, p.idParceiro, p.comissao ,p.NProcesso, p.dataPedido , p.nomeCliente, e.Descricao, p.dataVisita, i.nomeImovel, f.desc, p.file_certificadoProvisorio, p.adenePaga, (SELECT nome from users as u where u.id = p.idAgencia) as nomeAgencia, (SELECT nome from users as u where u.id = p.idParceiro) as nomeParceiro FROM processo as p LEFT JOIN imovel as i on i.idImovel = p.idImovel LEFT JOIN familia as f on f.idTipologia = p.idTipologia LEFT JOIN estadoscertificado as e on e.CodEstadosCertificado = p.idEstado ORDER BY p.NProcesso desc'));
Related
can I get the role_ID of a user in a course with a SQL Statemant?
I also want to know what is the role of the user in the course?
Thank you for your help!
SELECT r.id, r.shortname
FROM mdl_role_assignments AS ra
LEFT JOIN mdl_user_enrolments AS ue ON ra.userid = ue.userid
LEFT JOIN mdl_role AS r ON ra.roleid = r.id
LEFT JOIN mdl_context AS c ON c.id = ra.contextid
LEFT JOIN mdl_enrol AS e ON e.courseid = c.instanceid AND ue.enrolid = e.id
WHERE ue.userid = ? AND e.courseid = ?;
i want to translate this code in phpmyadmin to laravel
SELECT AVG(valeur_5), id_client, nom_propriete, prenom_propreite, nom_entreprise, type_utilisatuer
FROM cliets c
INNER JOIN evaluations e on c.id = e.id_client
INNER JOIN users u on u.id = c.id_user
INNER JOIN propreites p on p.id_user = u.id
GROUP BY id_client
If you want to embed raw SQL into your program, just wrap it in a DB::select.
DB::select("SELECT AVG(valeur_5), id_client, nom_propriete, prenom_propreite, nom_entreprise, type_utilisatuer FROM cliets c INNER JOIN evaluations e on c.id = e.id_client INNER JOIN users u on u.id = c.id_user INNER JOIN propreites p on p.id_user = u.id GROUP BY id_client")
Alternatively, you can rewrite it with the Eloquent query builder.
try this:
$orders = DB::table('hrayfis')
->join('evaluations','evaluations.id_herfi','hrayfis.id')
->join('users','users.id','hrayfis.id_user')
->join('propreites','propreites.id_user','users.id')
->select(
DB::raw('avg(valeur_5)','propreites.nom_propriete '))
->groupBy('hrayfis.id')->get();
I have created one SQL sub-query which was running perfectly.
SELECT o.*, u.*,OD.* FROM orders o
LEFT JOIN order_details OD on o.id = OD.order_id
LEFT JOIN users u ON o.user_id = u.id
WHERE o.order_status = 1 AND o.id NOT IN
(SELECT r.order_id FROM order_rejected_details r WHERE r.courier_id =$courierId)
order by o.id desc
How to write above query with NOT in condition in CakePHP?
You can simply write this query in CakePHP as per below.
In model:
$resultData = $this->query('SELECT o.*,
u.*,
OD.*
FROM orders o
LEFT JOIN order_details OD
ON o.id = OD.order_id
LEFT JOIN users u
ON o.user_id = u.id
WHERE o.order_status = 1
AND o.id NOT IN (SELECT r.order_id
FROM order_rejected_details r
WHERE r.courier_id = $courierid)
ORDER BY o.id DESC');
return $resultData;
So I have two MySQL queries that if I had the knowledge to combine I would but I don't so that's why I turned to "SO", and in that case I haven't tried anything because its out of my scope. I want to combine all in one query and if that's not possible please let me know.
Query one "This selects all of your friends posts including yours":
"SELECT b.*, c.photo, d.name, e.status
FROM post b
INNER JOIN profile c
INNER JOIN user d
INNER JOIN user_friendship e
ON b.from_user = c.user_id
AND b.from_user = d.id
AND e.friend_id = b.from_user
WHERE e.status = :status
AND e.user_id = :id
ORDER BY b.id DESC LIMIT 20"
Query two "This selects all of the people your following posts":
"SELECT b.*, c.photo, d.name, e.status
FROM post b
INNER JOIN profile c
INNER JOIN user d
INNER JOIN user_follower e
ON b.from_user = c.user_id
AND b.from_user = d.id
AND e.to_id = b.from_user
WHERE e.status = :status
AND e.who_id = :id
ORDER BY b.id DESC LIMIT 20"
I have combined these but with php alone. I'd like to combine both in one single MySQL query. Thanks in advance
SELECT *
FROM
(SELECT b.*,
c.photo,
d.name,
e.status
FROM post b
INNER JOIN profile c
INNER JOIN USER d
INNER JOIN user_friendship e ON b.from_user = c.user_id
AND b.from_user = d.id
AND e.friend_id = b.from_user
WHERE e.status = :status
AND e.user_id = :id LIMIT 20
UNION SELECT b.*,
c.photo,
d.name,
e.status
FROM post b
INNER JOIN profile c
INNER JOIN USER d
INNER JOIN user_follower e ON b.from_user = c.user_id
AND b.from_user = d.id
AND e.to_id = b.from_user
WHERE e.status = :status
AND e.who_id = :id LIMIT 20 ) MainQuery
ORDER BY id DESC
SELECT b.*, c.photo, d.name, e.status, "P"
FROM post b
INNER JOIN profile c
INNER JOIN user d
INNER JOIN user_friendship e
ON b.from_user = c.user_id
AND b.from_user = d.id
AND e.friend_id = b.from_user
WHERE e.status = :status
AND e.user_id = :id
ORDER BY b.id DESC LIMIT 20
UNION
SELECT b.*, c.photo, d.name, e.status, "F"
FROM post b
INNER JOIN profile c
INNER JOIN user d
INNER JOIN user_follower e
ON b.from_user = c.user_id
AND b.from_user = d.id
AND e.to_id = b.from_user
WHERE e.status = :status
AND e.who_id = :id
ORDER BY b.id DESC LIMIT 20
With the extra column you can see where the row is coming from (P = post, F = following)
Keep in mind that union will remove duplicate rows. If you want to see all rows use UNION ALL
I have a select statement like this:
SELECT c.id AS courseid, u.id AS userid
FROM
mdl_user_enrolments ue
join mdl_enrol e on e.id = ue.enrolid
join mdl_user u on u.id = ue.userid
join mdl_course c on c.id = e.courseid
I have another piece of data I want that does not have a direct relationship with the current tables. I have been calling this function in the output loop,
function getLastCourseAccessByUser($courseid, $userid){
global $DB;
return
$DB->get_record_sql('
SELECT FROM_UNIXTIME(time, "%m/%d/%Y") as ftime
FROM mdl_log
WHERE course = '.$courseid.' AND userid = '.$userid.'
ORDER BY time DESC
limit 1
');
}
but would rather get all the data once, and not call back the db.
I am trying this:
SELECT c.id AS courseid, u.id as userid
(
SELECT FROM_UNIXTIME(time, "%m/%d/%Y") as ftime
FROM mdl_log
WHERE course = c.id AND userid = u.id
) AS lastaccessdate
FROM
mdl_user_enrolments ue
join mdl_enrol e on e.id = ue.enrolid
join mdl_user u on u.id = ue.userid
join mdl_course c on c.id = e.courseid
Or, could I call the function directly from the sql like this:
SELECT c.id AS courseid, u.id as userid,
'.getLastCourseAccessByUser(c.id,u.id).' AS lastaccessdate
FROM
mdl_user_enrolments ue
join mdl_enrol e on e.id = ue.enrolid
join mdl_user u on u.id = ue.userid
join mdl_course c on c.id = e.courseid
Thank you.
SELECT c.id AS courseid, u.id AS userid, FROM_UNIXTIME(t.time, "%m/%d/%Y") as ftime
FROM
mdl_user_enrolments ue
join mdl_enrol e on e.id = ue.enrolid
join mdl_user u on u.id = ue.userid
join mdl_course c on c.id = e.courseid
join mdl_log t on t.course = c.id and t.userid = u.id
You can put AND and OR clauses in a join in mysql so you can join on both your conditionals. Though you may want to make it an outer join so that it won't fail the entire query if the conditionals aren't met
Otherwise your sub select option should also work (this one)
SELECT c.id AS courseid, u.id as userid
(
SELECT FROM_UNIXTIME(time, "%m/%d/%Y") as ftime
FROM mdl_log
WHERE course = c.id AND userid = u.id
) AS lastaccessdate
FROM
mdl_user_enrolments ue
join mdl_enrol e on e.id = ue.enrolid
join mdl_user u on u.id = ue.userid
join mdl_course c on c.id = e.courseid
The more data you process on the MySQL Server side, the less traffic is generated between the MySQL Server and your PHP application, thus speeding up the application. I would say to get all the data from the server at once and not go back and forth too many times.
You can also create the getLastCourseAccessByUser function as a stored function in MySQL if you need to re-use it in other applications:
DELIMITER \\
DROP FUNCTION IF EXISTS getLastCourseAccessByUser\\
CREATE FUNCTION getLastCourseAccessByUser (in_course_id INT, in_user_id VARCHAR(50)
RETURNS STRING
BEGIN
SELECT FROM_UNIXTIME(time, "%m/%d/%Y") AS ftime
FROM mdl_log
WHERE course = in_course_id AND userid = in_user_id
ORDER BY time DESC
LIMIT 1
END\\
DELIMITER ;
Then you could use it in your SELECT statement:
SELECT c.id AS courseid,
u.id as userid,
getLastCourseAccessByUser(c.id,u.id) AS lastaccessdate
FROM mdl_user_enrolments ue
JOIN mdl_enrol e on e.id = ue.enrolid
JOIN mdl_user u on u.id = ue.userid
JOIN mdl_course c on c.id = e.courseid