Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I am working a website.And i want this query for a particular functionality.I have tried it but it is not working.
how to retrieve last 2 rows from a table in MySQL??
I want a query in MySQL.
thanks in advance.
SELECT users.* FROM users ORDER BY users.id DESC LIMIT 2
select id, type, details from supportContacts ORDER BY id DESC LIMIT 2
ORDER BY id DESC LIMIT 2 helps you retrieve last two rows
Whats your table structure?
is there any date time field to get last records?
Is there any primary key?
If primary key then you could use:
Select * from table_name order by primary_key_column desc limit 2;
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I want to fetch random record from the table but repeated game id record is will become out only once at a time. I am sending a screenshot of my table
You need to use MySQL RAND() function.
Query:
SELECT * FROM lessons_game ORDER BY RAND() LIMIT 1
// Put proper fields instead of *
Explanation:
ORDER BY RAND() will shuffle records random records from DB Table.
And we will get random records after we execute the query result set.
We are putting LIMIT 1 so only one record will be pulled out.
Try the following command
SELECT * FROM lessons_game ORDER BY RAND() LIMIT 1
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to create a database that holds all my information with questions, users, answers etc.
I've been thinking all day how to solve this problem of mine. Okey, so the problem is like this:
Let's say I have a table in my DB, with questions (300++) and the user that logs on will get a random question shown. But never the same question again, so I'll need to store this information in a separate table with the user-ID and the question-ID. And I'll need to create another table that stores the answers to the questions.
So how would this PHP/MYSQL-code look like? Because I'll need to find a random question that hasnt been shown to the same user again.
If something is unclear, please let me know. And thanks in advance
You can use RAND() function and NOT IN
Example:
SELECT * FROM `questions` WHERE id NOT IN (5, 3) ORDER BY RAND() LIMIT 1;
You have to fetch question id using sub query
Example
SELECT * FROM `questions` WHERE id NOT IN (SELECT question_id FROM shown_questions WHERE user_id=1) ORDER BY RAND() LIMIT 1;
You can use an outer join to select items that have not been used already. It might be a bit faster than NOT IN if you set up the indexes:
SELECT questions.* FROM questions
LEFT JOIN shown_questions
ON (questions.id=show_questions.question AND user_id=42)
WHERE shown_questions.question IS NULL
ORDER BY RAND() LIMIT 1;
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to select same values in 1 column, for example:
Select * from Table, this needs to return like this:
ColumnName | ColumnValue
google 1,2,3,4
facebook 1,2,3,5
The actual query, returns this.
ColumnName | ColumnValue
google 1
google 2
google 3
google 4
facebook 1
facebook 2
facebook 3
facebook 4
Can someone help me?
Thanks!
You're looking for GROUP_CONCAT:
SELECT GROUP_CONCAT(ColumnValue SEPARATOR ', ') FROM table GROUP BY ColumnName;
Considering the reasoning behind relational databases though, you should be cautious about purposely denormalizing your results.
It sounds like you are looking for GROUP_CONCAT()
SELECT ColumnName, GROUP_CONCAT(ColumnValue) AS `ColumnValues`)
FROM Table
GROUP BY ColumnName
You need to use the GROUP_CONCAT and GROUP BY function:
SELECT ColumnName, GROUP_CONCAT(ColumnValue) AS ColumnValues FROM Table GROUP BY ColumnName
If you don't want allow duplicate values in the list such as 1,2,2,2,3,4 then add DISTINCT:
SELECT ColumnName, GROUP_CONCAT(DISTINCT ColumnValue) AS ColumnValues FROM Table GROUP BY ColumnName
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have pwd_history table containing columns id(PK),old_password, created_date,user_id. When ever user modify the password an extra row will be added to that table.I want a single query through which it should verify the entered password is in last three passwords modified by the user. Is there a way in Mysql? thanks.
If the password was hashed with mysql's default password, then
try this, with valid input values:
Updated:
select * from (
select * from old_pwds_table
where user_id=input_id
order by created_date desc
limit 3
) a
where
old_password=password('new password')
Former answer:
select * from old_pwds_table
where user_id=input_id
and old_password=password('new password')
order by created_date desc
limit 3
Check this function hope It helps to you:
function check_old_pass($new_pwd){
$result=mysql_num_rows(mysql_query("select * from old_pwds_table
where user_id=input_id
and old_password=$new_pwd
order by created_date desc
limit 0,3"));
if ($result==1) {
return true;
} else {
return false;
}
}
SELECT
*
FROM
pwd_history
WHERE
user_id = 'USER_ID'
AND
oldpasswords = 'NEW_PASSWORD'
ORDER BY
created_date DESC
LIMIT 3
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Everybody. I have a problem with my assignment about hotel booking online, that my problem is difficult to check available room. So I have many table like:
tbRoom(RoomID,RoomName,...)
tbReservation(ResID,ResDate,....)
tbReservationDetail(ResID,RoomID,ArrvialDate,DepartureDate,....)
I want to get Room that available in between two date.
so, How I do?or DO I need to create more table?
Please help me, Thank for helping....
You can select your reserved rooms and invert:
SELECT * FROM tbRoom WHERE RoomID NOT IN (SELECT RoomID FROM tbReservationDetail WHERE ArrvialDate < yourDate AND DepartureDate > yourDate)
You can achieve your result using above 3 tables you specified. but only thing you have to change is that you have to use RoomID filed in tbReservation table also.
rbRoom -> store the distinct rooms
tbReservation -> when a room is reserved this table will hold the RoomID and Reservation ID
tbReservationDetail ->stores the other details regarding arrivalDate,DepartureDate etc.
then follow the below query
SELECT * FROM tbRoom WHERE RoomID NOT IN (SELECT RoomID FROM tbReservation
tr,tbReservationDetail td WHERE td.ArrvialDate < yourDate AND
td.DepartureDate > yourDate and tr.RoomID=td.RoomID)