The following will work if each of the three tables yields results, however if just one of these tables chucks nothing, then it breaks the query and doesn't delete anything. Is there a way to bulletproof this query so if just 1 of the three tables has results, they will be deleted?
DELETE e, f, c
FROM `users exercises` e
inner join `users foods` f on f.userid = e.userid and f.`date` = e.`date`
inner join `users check-ins` c on c.userid = e.userid and c.`date` = e.`date`
WHERE e.`date` = '$date' AND e.userid = '$user->id'
Do you have a 'users' table? If so, then you can write something like this:
DELETE e, f, c
FROM 'users' u
left join 'users exercises' e on e.userid = u.userid and e.`date` = '$date'
left join 'users foods' f on f.userid = u.userid and f.`date` = '$date'
left join 'users check-ins' c on c.userid = u.userid and c.`date` = '$date'
WHERE u.userid = '$user->id'
Related
When I use the PHP $qs variable in the WHERE condition it works fine but in LEFT JOIN condition, it's not returning any data.
Problem Line: AND e.terms_id_user =
My Query: (Working)
SELECT
*,
'users_staff_driving_license' AS driving_license_link,
DATE_FORMAT(terms_date_created,'%a %d-%M-%Y %r') AS udate_created,
DATE_FORMAT(terms_date_updated,'%a %d-%M-%Y %r') AS udate_updated
FROM users AS a
LEFT JOIN users_staff_details AS b
ON a.user_id = b.users_staff_id_user
LEFT JOIN user_access AS c
ON a.user_id_access_level = c.user_access_id
LEFT JOIN businesses AS d
ON a.user_id_client = d.business_id
LEFT JOIN terms AS e
ON e.terms_id_client = 'AA0000001'
AND e.terms_id_store = '1'
AND e.terms_id_user = 1001
AND e.terms_datatype = 'users_staff_driving_license'
WHERE
a.user_id = '$qs'
My Query: (Not Working)
SELECT
*,
'users_staff_driving_license' AS driving_license_link,
DATE_FORMAT(terms_date_created,'%a %d-%M-%Y %r') AS udate_created,
DATE_FORMAT(terms_date_updated,'%a %d-%M-%Y %r') AS udate_updated
FROM users AS a
LEFT JOIN users_staff_details AS b
ON a.user_id = b.users_staff_id_user
LEFT JOIN user_access AS c
ON a.user_id_access_level = c.user_access_id
LEFT JOIN businesses AS d
ON a.user_id_client = d.business_id
LEFT JOIN terms AS e
ON e.terms_id_client = 'AA0000001'
AND e.terms_id_store = '1'
AND e.terms_id_user = '$qs'
AND e.terms_datatype = 'users_staff_driving_license'
WHERE
a.user_id = '$qs'
Subquery returns more than 1 row and this is the error. I want to print all the booked room details showing the client id and the owner id.
$query = "SELECT r.room_id, r.roomsize, r.numberroom, r.price, u.username,
(SELECT u.username FROM room_register r
INNER JOIN user_info u on r.owner_id = u.user_id
INNER JOIN booked b on b.room_id = r.room_id)as owner_name
FROM room_register r INNER JOIN booked b on b.room_id = r.room_id
INNER JOIN user_info u on u.user_id = b.booked_by";
You have too many joins in the subquery:
SELECT r.room_id, r.roomsize, r.numberroom, r.price, u.username,
(SELECT u.username
FROM user_info u
WHERE r.owner_id = u.user_id
) as owner_name
FROM room_register r INNER JOIN
booked b
ON b.room_id = r.room_id INNER JOIN
user_info u
ON u.user_id = b.booked_by;
Or just use two separate JOINs:
SELECT r.room_id, r.roomsize, r.numberroom, r.price, u.username,
uo.username as owner_name
FROM room_register r INNER JOIN
booked b
ON b.room_id = r.room_id INNER JOIN
user_info u
ON u.user_id = b.booked_by LEFT JOIN
user_info uo
ON uo.user_id = r.owner_id
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
so i have 5 tables in which it is interconnected with foreign keys
and here is a sample output of table 3
what i wanted to do in table number 3 is to extract the SubdeptID of user with userid of 10 but in this case it has 2 userid10 so its print both. what i want to print is only the one with latter TransferID. my select statement is this
$sql_exp = "SELECT a.UserID, b.Employeename, c.TransferID, e.Department
FROM dbo.FA_Laptop a
INNER JOIN dbo.users b
on a.UserID = b.UserID
INNER JOIN dbo.SubDeptTransfer c
ON a.UserID = c.UserID
INNER JOIN dbo.SubDept d
ON c.SudDeptID = d.SubDeptID
INNER JOIN dbo.departments e
ON d.DeptID = e.DeptID
WHERE a.FAID = '$faidf' ORDER by c.TransferID DESC LIMIT 1";
my php code is
$rs = $conn->Execute($sql_exp);
if ($rs->EOF) {
echo "<tr><td>Please check for the Employee Name or the Department</td>";
} else {
while (!$rs->EOF){
echo "<tr><td>".$rs->Fields("Department")." / ".$rs->Fields("EmployeeName")."</td>";
$rs->movenext();
}
$rs->Close();
}
im having an error in "LIMIT" query.
MSSQL don't have LIMIT keyword.
Use TOP instead LIMIT.
$sql_exp = "SELECT TOP 1 a.UserID, b.Employeename, c.TransferID, e.Department
FROM dbo.FA_Laptop a
INNER JOIN dbo.users b
on a.UserID = b.UserID
INNER JOIN dbo.SubDeptTransfer c
ON a.UserID = c.UserID
INNER JOIN dbo.SubDept d
ON c.SudDeptID = d.SubDeptID
INNER JOIN dbo.departments e
ON d.DeptID = e.DeptID
WHERE a.FAID = '$faidf' ORDER by c.TransferID DESC";