This question already has answers here:
How do I calculate the offsets for pagination?
(4 answers)
How to apply bindValue method in LIMIT clause?
(11 answers)
Closed 5 years ago.
I asked a question yesterday titled "How do i use binded variables with MySql LIMIT clause. Sorry, I guess it was not clear enough and got 30 views but no suggestions. So here is what I found after a day of trial and error. First, I discovered that the the LIMIT is the actual physical position of a record in a table not my auto_incremented id_ number. A lot of documentation i read also indicated it was the OFFSET that held the record position, so I wasted a lot of time with weird results. Second, I found documentation with a couple of ways to bind a variable to LIMIT. None of them worked, so I resorted to assigning a $variable to the LIMIT as follows.
("SELECT * FROM $livetable WHERE cust_zip = :cust_zip
HAVING distance < :mydistance ORDER BY client_id ASC LIMIT $start_record_position
, $how_many_records_to_display");
This works for me but any better suggestions, would be appreciated. And I hope this will save someone else a day of figuring this out.
Related
This question already has answers here:
MySQL - How to SUM times?
(5 answers)
Closed 5 years ago.
I am working on a check in out front desk register and hit a wall when attempting to add the total times for all visits by individuals or companies: when i run the following query:
$yearStart = date('Y-m-d', strtotime(date('Y-01-01')));
$today = date("Y-m-d",time());
$sql = "SELECT visCompany, count(visCompany) as accumilatedVisits, sum(timeVisited) as total_time from cards where cards.dateOfVisit BETWEEN '$yearStart' AND '$today' GROUP BY cards.visCompany ORDER BY accumilatedVisits DESC";
I get the sum of the two numbers but not in the correct format, but I cannot figure or find a better way to do it... I had at the start the following times saved in my Cards table: 00:29:18 and 00:05:43 which when added together give me 3461 which would be correct-ish if you put a space between 34 and 61, the total should be 00:35:01 and the values seem to be there but i would think that there is a cleaner way of accomplishing the correctly added and formatted values... I have been scowering the mariadb kb and even trying to find the solution here... I would be okay figuring out a way to do the formatting and the minute correction in php too.. but would prefer to use sql to get the desired result.
Any one have any suggestions?
Whops i put a duplicate... found the answer:: MySQL - How to SUM times?
my new query:: SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(cards.timeVisited))) as total_time_visited FROM cards;
tested and worked with the result being 00:35:01.
Sorry about the duplicate... And thanks for reading.
This question already has answers here:
MySQL: Alternatives to ORDER BY RAND()
(9 answers)
Closed 6 years ago.
I have a MySQL database with 112 rows (with ID column from 1 to 112), I need to SELECT 6 random rows (doesn't matter if sequential) to show in PHP/HTML page and to be changed daily.
The only option I think is to depend on the current date.
Is there any solution ?!
Thank you ...
EDIT:
Question was solved. Although no one really understood what I want but I keep getting down votes.
The question was : 6 Random rows EVERY DAY not every page refresh or every call from DB.
But thanks for the amazing effort. Question solved.
The MySQL statement ORDER BY RAND() will order the matching rows randomly. Combined with LIMIT 6 you'll get six random rows.
See http://dev.mysql.com/doc/refman/5.7/en/mathematical-functions.html#function_rand for reference.
Just use ORDER BY RAND() to randomize the row order, then show the first 6:
SELECT * FROM Yourtable ORDER BY RAND() LIMIT 0,6;
Use following query
SELECT column FROM table
ORDER BY RAND()
LIMIT 6
This question already has answers here:
SELECT COUNT() vs mysql_num_rows();
(3 answers)
Closed 7 years ago.
I wan to know the difference between getting row count via select COUNT(*) from table fetching data AND mysqli_num_rows($ofquery)(in php)?
I tried in both ways and works well. But method is faster?
Use COUNT, internally the server will process the request differently.
When doing COUNT, the server will only allocate memory to store the result of the count.
When using mysqli_num_rows, the server will process the entire result set, allocate memory for all those results, and put the server in fetching mode, which involves a lot of different details, such as locking.
Think of it like the following pseudo scenario:
1) Hey , how many people are in the class room? (count)
2) Hey , get me a list of all the people in the classroom, ... I'll calculate the number of people myself (mysqli_num_rows)
count is the best than mysqli_num_rows
Reference from here.
This question already has answers here:
How can I return 10 of the most recent results in sql?
(4 answers)
Closed 9 years ago.
How can i ask a question to a mysql-database (via PDO) to return the 3 latest added rows? The id column is key and auto incremented. That means that highest id means latest added. Don't take into account the fact that rows can have been deleted and such.
Could i somehow use * and LIMIT 3 and start from the bottom some way - or something?
Should be fairly easy but i am kind of stuck.
Order by ID desc limit 3
gives you the three rows with the highest ID. Those are not necessarily the latest three added rows. But they are the latest three added rows still existing in the table.
This question already has answers here:
How to put more than 1000 values into an Oracle IN clause [duplicate]
(11 answers)
Closed 9 years ago.
I am using an IN clause, whereby, it needs to search the id IN these set of IDs that are not in order, meaning, it came from a different criteria., how to solve this ?
SELECT DISTINCT ID FROM "projeck"."mytable" "t" WHERE staffID IN (75953,196262,196387,133585,195639,196702,195790,195820,192903,145383,179603,175896,176554,43545,154843,183798,195767,195715,etc..etc.. etc..)
and i am getting this oracle error
General error: 1795 OCIStmtExecute: ORA-01795: maximum number of expressions in a list is 1000
My first choice would be to reference the function that generates these values directly.
If the values were being used for multiple queries and were expensive to calculate then I'd think about loading them into a global temporary table and joining to it.