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)
Related
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;
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 a table of users and I want a query that will tell me if a particular user is listed in the table. For example, in the table below:
Users | Ages
Bob | 22
Mike | 30
Sue | 21
Can someone help me with a query that does something like,
If (In USERS_TABLE, Mike is in column 'Users'){$userPresent="true";}
Mysql Num rows can help you with that
$query = mysql_query("select * from users_table where Users = 'Mike'");
if(mysql_num_rows($query))
{
echo "user present";//use your code how you want
}
else
{
echo "user not present";
}
mysql_ extension is deprecated as of PHP 5.5.0.
So Please choose PDO or MYSQLI api for better experience.
Your query would be:
SELECT * FROM USERS_TABLE WHERE Users='Mike'
You could run a query like
SELECT * FROM USERS_TABLE WHERE Users='Mike'
Then, you could check if the number of rows returned from that query was more than 0.
SELECT count(*) as count FROM USERS_TABLE WHERE Users='Mike'
If count>0 then user exists.
or if you want that user record,just use as below
SELECT * FROM USERS_TABLE WHERE Users='Mike'
Select * FROM USERS_TABLE WHERE Users = 'mike', here you can choose specific table like
Select Users FROM USERS_TABLE WHERE Users= 'mike'
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
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
Improve this question
I have string like this '1~5~14~62~53~26' in a sql column, actually these are values of a user selected list. Now i want to check if the given string contains specific value like '5' exist or not how can i achieve this in sql query?
SQL Fiddle
MySQL 5.6.6 m9 Schema Setup:
CREATE TABLE mytable
(
id int auto_increment primary key,
cities varchar(20)
);
INSERT INTO mytable
(cities)
VALUES
('5~1~23~45~9~8~71'),
('2~22~39~8~23~5'),
('2~22~39~8~5~9'),
('2~22~39~8~23');
Query 1:
SELECT `cities` FROM `mytable` where `cities` REGEXP '^5~|~5$|~5~'
Results:
| CITIES |
|------------------|
| 5~1~23~45~9~8~71 |
| 2~22~39~8~23~5 |
| 2~22~39~8~5~9 |
More about REGEXP and RLIKE
You could use something like
SELECT * from MyTable where locate(concat('~','5','~'), concat('~',`MyColumn`,'~')) != 0;
It's horrible, and prone to error.
The real solution is to normalise your data, which in this case means put your user-selected list entries in a separate table, indexed by user id.
Is it in MySQL?
Then use REGEXP for example:
SELECT * FROM `table_name` WHERE `col_name` REGEXP '[5]';
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
Guys im having a issue with my mysql php problem ok i want this to pull table information out of my mysql database in it shows up the firstname by the number 10 but i want to add more too it like 10 100 1000 in other numbers how can i do this from this code
$result = mysqli_query($con,"SELECT * FROM tvshows
WHERE FirstName='10',100,1000");
SELECT * FROM tvshows WHERE FirstName IN (10,100,1000);
You can look here:
http://dev.mysql.com/doc/refman/5.0/en/any-in-some-subqueries.html
and here:
http://dev.mysql.com/tech-resources/articles/subqueries_part_1.html
If you want to select strings, you need to '', ex.:
SELECT * FROM tvshows WHERE FirstName IN ('10','100','1000');
If Numbers like this:
SELECT * FROM tvshows WHERE FirstName IN (10,100,1000);
The IN operator allows you to specify multiple values in a WHERE clause.
$result = mysqli_query($con,"SELECT * FROM tvshows
WHERE FirstName IN('10','100','1000')");
See Example
You have to change the = for IN
$result = mysqli_query($con,"SELECT * FROM tvshows WHERE FirstName IN(10,100,1000);
SQL IN clause documentation