I have this code that (without the WHERE, was working) How do I get it to work with the WHERE clause ?
I just need it to only list lines that is current and max 2 years ahead.
$SQL = "SELECT ";
$SQL .= "SUM(Bookings.Spots) as SUMSPOT, Trips.ID, Bookings.FK_ID, Trips.MaxSpots, ";
$SQL .= "Trips.Tripnr, Trips.StartDate, Trips.EndDate, Trips.StartLocation, ";
$SQL .= "Trips.DestinationDK, Trips.PricePerSpot ";
$SQL .= "FROM Trips WHERE Trips.EndDate >= NOW() AND Trips.EndDate < DATE_ADD(NOW(), INTERVAL 2 YEAR) ";
$SQL .= "LEFT JOIN Bookings on Bookings.FK_ID = Trips.ID ";
$SQL .= "GROUP BY Trips.ID, Bookings.FK_ID ORDER BY Trips.StartDate ASC ";
You need to add the WHERE clause after the LEFT JOIN and before the GROUP tag.
You can check the documentation here for more answers as to where you can put which keyword.
Related
I would like to get data from two tables in MySQL, ordered by date.
$sql = "
SELECT
items.*, invoice.*
FROM
items
JOIN
invoice
ON
items.user_id = invoice.buyer_id
WHERE
items.user_id = '$user_id'"
LIMIT
10
ORDER BY
date;
";
Try with:
$sql = "SELECT *";
$sql .= " FROM items, invoice";
$sql .= " WHERE items.user_id = invoice.buyer_id";
$sql .= " AND items.user_id = '$user_id'";
$sql .= " ORDER BY date DESC";
$sql .= " LIMIT 10";
Also it is better if you use it as a prepared statement instead of having the variable inside the SQL query to avoid SQL injection.
I'm making a feed wall, and two services save posts to the same database table.
One posting service is used way more than the other, so on the wall I want to limit each service to the 25 newest posts (total 50) on the front page for equal representation.
This is what I originally had, without evening the posts:
$sql = "SELECT * FROM posts";
$sql .= " WHERE disq = 0";
$sql .= " AND approved = 1";
$sql .= " ORDER BY created_at DESC";
$sql .= " LIMIT 50";
but then I try to limit them by service:
$sql_1 = "SELECT * FROM posts";
$sql_1 .= " WHERE disq = 0";
$sql_1 .= " AND approved = 1";
$sql_1 .= " AND source = 'TW'";
$sql_1 .= " ORDER BY created_at DESC";
$sql_1 .= " LIMIT 25";
$sql_2 = "SELECT * FROM posts";
$sql_2 .= " WHERE disq = 0";
$sql_2 .= " AND approved = 1";
$sql_2 .= " AND source = 'IG'";
$sql_2 .= " ORDER BY created_at DESC";
$sql_2 .= " LIMIT 25";
Doing something like
$sql = $sql_1 UNION $sql_2;
Doesn't seem to work, because all the examples I see perform LIMIT at the end of a bunch of queries. And an ORDER BY should be performed after that, to reorder the posts chronologically and make the wall mixed service.
MySQL help is appreciated
You could omit your ORDER BY in your subqueries and just do it at the end, after the union.
(
SELECT * FROM POSTS
WHERE disq = 0 AND approved = 1 AND source= 'TW'
LIMIT 25
)
UNION ALL
(
SELECT * FROM POSTS
WHERE disq = 0 AND approved = 1 AND source= 'IG'
LIMIT 25
)
ORDER BY created_at DESC
More info on the union and how to sort is explained at this MySQL documentation
You need to pull the most recent 25 items separately from your two criteria sets, then put them together and order them again.
That will go like this. You need these parenthetical subqueries because the ORDER BY ... LIMIT clauses need to be associated with each one separately.
SELECT *
FROM
( SELECT *
FROM POSTS
WHERE disq = 1 AND approved = 1 AND source= 'TW'
ORDER BY created_at DESC
LIMIT 25
) A
UNION ALL
( SELECT *
FROM POSTS
WHERE disq = 1 AND approved = 1 AND source= 'IG'
ORDER BY created_at DESC
LIMIT 25
) A
ORDER BY created_at DESC, source
An index on your POSTS table on (disq, approved, source, created_at) will serve to speed up this query.
What I'm trying to return in a set: The lessonIDs associated to the userID and schoolID within a given startTime and endTime.
This is to be displayed on a calendar. I only want to display the lessons that are associated to the user viewing it for a selected school within the time range they are viewing on the calendar.
This data is just dummy data to show the table structure and data types.
Lesson Table:
lessonID roomFK startTime endTime
1 2 2014-08-01 13:00:00 2014-08-01 14:00:00
2 3 2014-08-01 13:00:00 2014-08-01 14:00:00
Room Table:
roomID schoolFK
1 1
2 1
3 2
4 2
School Table:
schoolID
1
2
User Table:
userID
1
2
3
Lesson/Teacher Junction Table:
lessonFK teacherFK
1 1
1 2
2 3
What I've tried:
function find_lessons_by_school_id_and_teacher_id_ranged_feed($school_id, $teacher_id, $start, $end) {
global $connection;
$school_id = mysqli_real_escape_string($connection, $school_id);
$teacher_id = mysqli_real_escape_string($connection, $teacher_id);
$start = mysqli_real_escape_string($connection, $start);
$end = mysqli_real_escape_string($connection, $end);
$start = strftime("%Y-%m-%d %H:%M:%S", $start);
$end = strftime("%Y-%m-%d %H:%M:%S", $end);
$query = "SELECT * ";
$query .= "FROM lesson ";
$query .= "JOIN room ON lesson.roomFK = room.roomID ";
$query .= "JOIN school ON room.schoolFK = school.schoolID ";
$query .= "JOIN user ON user.userID = junc_lesson_teacher.teacherFK ";
$query .= "JOIN junc_lesson_teacher ON junc_lesson_teacher.lessonFK = lesson.lessonID ";
$query .= "WHERE room.schoolFK = '{$school_id}' ";
$query .= "AND lesson.startTime >= '{$start}' ";
$query .= "AND lesson.endTime <= '{$end}' ";
$query .= "AND user.userID = '{$teacher_id}' ";
$query .= "ORDER BY lesson.roomFK ASC";
$set = mysqli_query($connection, $query);
return $set;
}
When I run this, mySQL complains about the query statement.
Any input on how to do this properly would be greatly appreciated! Thanks!
The order of your join operations is wrong. You've got to swap the last two ones. Instead of
$query .= "FROM lesson ";
$query .= "JOIN room ON lesson.roomFK = room.roomID ";
$query .= "JOIN school ON room.schoolFK = school.schoolID ";
$query .= "JOIN user ON user.userID = junc_lesson_teacher.teacherFK ";
$query .= "JOIN junc_lesson_teacher ON junc_lesson_teacher.lessonFK = lesson.lessonID ";
write
$query .= "FROM lesson ";
$query .= "JOIN room ON lesson.roomFK = room.roomID ";
$query .= "JOIN school ON room.schoolFK = school.schoolID ";
$query .= "JOIN junc_lesson_teacher ON junc_lesson_teacher.lessonFK = lesson.lessonID ";
$query .= "JOIN user ON user.userID = junc_lesson_teacher.teacherFK ";
because every table you use in a join must be introduced first. By your statement you can't use junc_lesson_teacher to join the user table, so you've got to join junc_lesson_teacher before you join user. Think of your tables as a chain without interrupts.
If you had a moderately sensible naming policy, that query might look like this...
$query = "
SELECT columns
, i
, actually
, want
FROM lesson l
JOIN room r
ON r.room_id = l.room_id
JOIN school s
ON s.school_id = r.school_id
JOIN lesson_teacher lt -- <-- NOTE TABLES SWAPPED AROUND
ON lt.lesson_id = l.lesson_id
JOIN user u
ON u.user_id = lt.teacher_id -- <-- SEE?
WHERE r.school_id = $school_id
AND l.startTime >= '$start'
AND l.endTime <= '$end'
AND u.user_id = $teacher_id
ORDER
BY l.room_id ASC;
";
$set = mysqli_query($connection, $query)
or die(mysqli_error($connection)); -- for development only
instead of
$query = "SELECT * ";
write all of required fields from all tables
$query = "SELECT lesson.roomFK,room.roomID,school.schoolID,room.schoolFK,user.userID,junc_lesson_teacher.teacherFK,junc_lesson_teacher.lessonFK,lesson.lessonID";
This question already has answers here:
mysqli_stmt::bind_result(): Number of bind variables doesn't match number of fields in prepared statement
(2 answers)
Closed 1 year ago.
I have the following mysql query which am executing in my php script:
$qr = "Select p.productID,p.productDesc,p.productQty,";
$qr .= "p.productPr,p.type,p.gender,p.date From";
$qr .= " products AS p";
$qr .= " INNER JOIN(Select c.productID, GROUP_CONCAT(";
$qr .= "DISTINCT c.availCol) AS color_list FROM";
$qr .= " availColors AS c GROUP BY c.productID) AS colors";
$qr .= " ON p.productID = colors.productID":
$qr .= " INNER JOIN(SELECT s.productID, GROUP_CONCAT";
$qr .= "(s.availSizes) AS size_list FROM availSizes AS s";
$qr .= " GROUP BY s.productID) AS sizes ON p.productID";
$qr .= " = sizes.productID";
$qr .= " INNER JOIN(SELECT avp.productID, avp.productImg";
$qr .= " FROM availImg AS avp ORDER BY avp.productID";
$qr .= " LIMIT 3) AS images ON images.productID = ";
$qr .= " p.productID";
$qr .= " WHERE p.productID = ?";
$qr .= " GROUP BY p.productID";
$stm = $mysqli->prepare($qr);
$stm->bind_param('s',$id);
$stm->execute();
$stm->store_result();
If($stm->num_rows == 1){
$stm->bind_result($pid,$desc,$qty,$pr,$type,$gender,
$date,$colID,$color,$sizeID,$sizes,$imgID,$imgUrl);
$stm->fetch();
......
}
When I execute the script I get an error saying:
Mysqli_stmt::bind_result(): Number of bind variables doesn't
Match number of fields in the prepared statement in...
I understand what this error means, but I have rechecked and recounted the number of bind variables in the bind_result() against the number of SELECTed columns, I just can't figure out what is wrong. I don't know how PHP evaluates values returned from a mysql subquery. Can anyone offer help on this? I have been on this all day! Thanks for any help!
$qr = "
Select
p.productID,
p.productDesc,
p.productQty,
p.productPr,
p.type,
p.gender,
p.date
From
products AS p
INNER JOIN(
Select
c.productID,
GROUP_CONCAT(DISTINCT c.availCol) AS color_list
FROM
availColors AS c GROUP BY c.productID) AS colors
ON
p.productID = colors.productID
INNER JOIN(
SELECT
s.productID,
GROUP_CONCAT(s.availSizes) AS size_list
FROM
availSizes AS s
GROUP BY
s.productID
) AS sizes
ON
p.productID = sizes.productID
INNER JOIN(
SELECT
avp.productID,
avp.productImg
FROM
availImg AS avp
ORDER BY
avp.productID
LIMIT
3) AS images
ON
images.productID = p.productID
WHERE
p.productID = ?
GROUP BY
p.productID";
Try formatting your query like this muti-line strings are perfectly fine in PHP, and it's almost impossible to make sense of it concatenated line by line. If I have it right you are missing at least a closing parentheses in there, the first inner join open parentheses is not closed.
You are only joining your other tables/columns not selecting the columns. Try adding the columns to the beginning of the query (before the FROM)
$qr = "SELECT p.productID,p.productDesc,p.productQty,";
$qr .= "p.productPr,p.type,p.gender,p.date,";
// colors columns
$qr .= "colors.productID, colors.color_list,";
// sizes columns
$qr .= "sizes.productID, sizes.size_list,";
// images columns
$qr .= "images.productID, images.productImg ";
$qr .= "From products AS p";
$qr .= " INNER JOIN(Select c.productID, GROUP_CONCAT(";
$qr .= "DISTINCT c.availCol) AS color_list FROM";
$qr .= " availColors AS c GROUP BY c.productID) AS colors";
$qr .= " ON p.productID = colors.productID":
$qr .= " INNER JOIN(SELECT s.productID, GROUP_CONCAT";
$qr .= "(s.availSizes) AS size_list FROM availSizes AS s";
$qr .= " GROUP BY s.productID) AS sizes ON p.productID";
$qr .= " = sizes.productID";
$qr .= " INNER JOIN(SELECT avp.productID, avp.productImg";
$qr .= " FROM availImg AS avp ORDER BY avp.productID";
$qr .= " LIMIT 3) AS images ON images.productID = ";
$qr .= " p.productID";
$qr .= " WHERE p.productID = ?";
$qr .= " GROUP BY p.productID";
I finally figured it out, thanks to MYSQL Workbench! I ran the query on mysql workbench and counted the number of columns returned, and declared variables for each on the bind_result().
Hi Guys i have this query and it is super slow.
If you can understand the code how can i possibly combine the two queries into one
or how can i possibly make this query faster.
this is the code
$strSQL = "SELECT user_status_history.*, queues_config.extension FROM user_status_history ";
$strSQL .= "LEFT JOIN queues_config ON user_status_history.skillset = REPLACE(queues_config.grppre, \":\", \"\") ";
$strSQL .= "WHERE user_status_history.status_id = 9 "; # outbound call
$strSQL .= "AND DATE_FORMAT(status_time, '%Y-%m-%d') = '$check_date' ";
if ($i==1)
$strSQL .= "AND TIME_FORMAT(status_time, '%H:%i') >= '$cboHrFrom:$cboMinFrom' ";
else if (strtotime($check_date) == strtotime($end_date))
$strSQL .= "AND TIME_FORMAT(status_time, '%H:%i') <= '$cboHrTo:$cboMinTo' ";
$strSQL .= "AND queues_config.extension = '$cboSkillSet' ";
if ($cboGroup!='' && $strAgentGroup!='') $strSQL .= "AND user_status_history.user_id IN (".$strAgentGroup.") ";
$strSQL .= "ORDER BY user_status_history.user_id, user_status_history.status_time ";
$rs2 = &$cn->Execute($strSQL);
while (!$rs2->EOF)
{
$strSQL = "SELECT status_time, time_to_sec(timediff(status_time, '".$rs2->fields['status_time']."')) AS timesecs FROM user_status_history ";
$strSQL .= "LEFT JOIN queues_config ON user_status_history.skillset = REPLACE(queues_config.grppre, \":\", \"\") ";
$strSQL .= "WHERE status_time > '".$rs2->fields['status_time']."' ";
$strSQL .= "AND user_id = '".$rs2->fields['user_id']."' ";
$strSQL .= "AND queues_config.extension = '".$rs2->fields['extension']."' ";
$strSQL .= "ORDER BY status_time LIMIT 1;";
$rs3 = &$cn->Execute($strSQL);
if (!$rs3->EOF)
{
$sum_talk_sec = $sum_talk_sec + $rs3->fields['timesecs'];
}
$rs3->Close();
$rs2->MoveNext();
}
$rs2->Close();
1
Check out there are table indexes for
user_status_history.status_id
user_status_history.user_id
2
these are difficult for optimizer. Try get rid of these
LEFT JOIN queues_config ON user_status_history.skillset = REPLACE(queues_config.grppre, \":\", \"\") ";
LEFT JOIN queues_config ON user_status_history.skillset = REPLACE(queues_config.grppre, \":\", \"\") ";
3
Get rid of the loop query.
Fetch all IDs from query1 to an array and create one query for them all:
"SELECT user_id, status_time, time_to_sec(timediff(status_time, '".$rs2->fields['status_time']."')) AS timesecs FROM user_status_history ";
...
AND user_id IN (id1, id2, id3 ... )
4 (From DCOder )
Convert these the other way around. Use PHP to create mysql datetime fields.
$strSQL .= "AND DATE_FORMAT(status_time, '%Y-%m-%d') = '$check_date' ";
-- >
preg_match('/^([0-9][0-9][0-9][0-9])-([0-9][0-9])-([0-9][0-9])$/', $check_date, $regs );
$enddate = date('Y-m-d', mktime( $hour=6, $min=0, $sec=0, $mon=$regs[2], $day=$regs[3]+1, $year=$regs[1] ));
$strSQL .= " AND status_time > '$check_date' AND status_time <= '$enddate' ;
$strSQL .= "AND TIME_FORMAT(status_time, '%H:%i') >= '$cboHrFrom:$cboMinFrom' ";
-- > $strSQL .= " AND status_time >= '$check_date $cboHrFrom:$cboMinFrom:00' ";
$strSQL .= "AND TIME_FORMAT(status_time, '%H:%i') <= '$cboHrTo:$cboMinTo' ";
-- > $strSQL .= " AND status_time <= '$check_date $cboHrTo:$cboMinTo:59' ";