I'm currently working on my query. So I have two tables, tbl_room and tbl_reservation. I wanted to do an update query with the following conditions:
Decrement room counter by 1 in tbl_room if:
The first and last name of the customer matches the first and last name in the tbl_reservation
The tbl_reservation room ID matches with the tbl_room room ID
I'm currently stuck with this:
$result = mysql_query("UPDATE
tbl_room
JOIN tbl_reservation
ON tbl_room.roomID = tbl_reservation.roomID
AND tbl_reservation.cus_fname = '$cusFN'
AND tbl_reservation.cus_lname = '$cusLN' SET tbl_room.reserve = reserve - 1 ");
These are my tables:
----------
tbl_room
----------
roomID,
room_type,
capacity,
reserve <--- reservation counter
----------
tbl_reservation
----------
reserveID,
cus_fname,
cus_lname,
I already came up with the right query. Yehey! So I moved my query just after the while statement and used this syntax:
$result6 = mysql_query("UPDATE tbl_room
JOIN tbl_reservation ON tbl_room.roomID=tbl_reservation.roomID
AND tbl_reservation.cus_fname='$cusFN'
AND tbl_reservation.cus_lname='$cusLN'
SET tbl_room.reserve=tbl_room.reserve-1");
Try this...
$sql = "UPDATE `tbl_room`
SET `tbl_room`.`reserve` = `tbl_room`.`reserve` - 1
WHERE `tbl_room`.`roomID` = `tbl_reservation`.`roomID`
AND `tbl_reservation`.`cus_fname` = '$cusFN'
AND `tbl_reservation`.`cus_lname` = '$cusLN'";
Related
I have two table where I want to query with join two table.
If there is pro_pic = 'NULL' than the result I want to get without null pro_pic.
Here is the Table:
user_info
ID user_id full_name country
--------------------------------------
1 Star01 Sagor Us
2 Star02 Rofiq India
3 Star03 Karim Aus
pro_pic
ID user_id pro_pic
---------------------------------
1 Star01 14523.png
2 Star02 NULL
3 Star03 554513.png
so I want to result like
ID user_id full_name country pro_pic
------------------------------------------------------------------
1 Star01 Sagor Us 14523.png
3 Star03 Karim Aus 554513.png
the null pro_pic field not showing.
Here is my query but I didn't got the result :'(
<?php
$stmt1 = mysqli_query($con,"SELECT ui.user_id, pp.pro_pic, pp.user_id,
ui.full_name, ui.country
FROM user_info
INNER JOIN pro_pic ON ui.user_id=pp.user_id where ui.show_hide_profile != '1' and pp.pro_pic != '' limit $current, $limit");
while($user_info = mysqli_fetch_array($stmt1)){
echo $user_id_view = $user_info['user_id'];
echo $full_name = $user_info['full_name'];
echo $country = $user_info['country'];
echo $pro_pic = $user_info['pro_pic'];
}
?>
Please help me thanks to all
Try this
$stmt1 = mysqli_query($con,"SELECT ui.user_id, pp.pro_pic, pp.user_id, ui.full_name, ui.country
FROM user_info ui
INNER JOIN pro_pic pp ON ui.user_id=pp.user_id where pp.pro_pic IS NOT NULL
ORDER BY ui.user_id limit $current, $limit");
EDIT: Added order by clause
Please note whenever you use limit clause you should use order by clause because if you are not using order by clause the MySQL result always return random rows
Use this query
$sql = "SELECT * FROM citys LEFT JOIN comments ON comments.city=citys.city WHERE citys.id=$id";
Here are the link to the code:
Join two mysql tables with php
Hope it helps.
Okay, I am trying to update a table depending if it belongs to the user, So In one table for example I have pictures, like so:
+----------------------------------------------------+
| picture_id | picture_user | picture_title |
+----------------------------------------------------+
and in another table I have:
+--------------------------------------------------------------+
| delete_pic_id | delete_pic_user | delete_pic_status |
+--------------------------------------------------------------+
now I want to update delete_pic_status only if the delete_pic_user matches the picture_user
Here's what I am thinking but need a little help:
$manage_application = $database->prepare(
"UPDATE deleted_pic
SET delete_pic_status=1
WHERE delete_pic_user(the other table)=the id");
How would I go about accomplishing this
Use Exists
UPDATE deleted_pic
SET delete_pic_status = 1
WHERE EXISTS (SELECT 1
FROM other_table ot
WHERE ot.picture_user = deleted_pic.delete_pic_user)
UPDATE deleted_pic SET delete_pic_status=1 WHERE
delete_pic_user in (select picture_user from myOthertable)
I assume you are using PDO and as you have the id of the user in a session variable you can use a parameterised query like this
$sth = $database->prepare( "UPDATE deleted_pic
SET delete_pic_status = 1
WHERE delete_pic_user = :id");
$result = $sth->execute(array(':id' => $_SESSION['userId']) );
if ( $result ) {
// the delete worked
} else {
// the delete failed
}
Below query will fulfill your need-
update all rows based on checking existence in another table
UPDATE deleted_pic dp
JOIN other_table ot ON ot.picture_user = dp.delete_pic_user
SET delete_pic_status = 1;
update one table's data in another table for all rows-
UPDATE deleted_pic dp
JOIN other_table ot ON ot.picture_user = dp.delete_pic_user
SET dp.column1 = ot.column5;
update one table's data in another table for specific rows-
UPDATE deleted_pic dp
JOIN other_table ot ON ot.picture_user = dp.delete_pic_user
SET dp.column1 = ot.column5
where dp.status=1;
UPDATE deleted_pic
SET delete_pic_status=1
WHERE deleted_pic.delete_pic_user in(SELECT picture_user from tab2)
I have a table that looks like this:
id | user_id | credits
----------------------
1 | 2 | 300
2 | 2 | 200
3 | 4 | 100
I need a select or way to add the credits from a specific user id
The select would be:
SELECT credit FROM myTable WHERE user_id ='2'
The result I would need in this case would be 500
How can I do this?
Use SUM() in your query will solve the issue.
SELECT SUM(`credits`) AS `total` FROM `myTable` WHERE `user_id` =2
Since you specify PHP tag, the below code will help you.
$mysqli=mysqli_connect("hostname","username","password","databasename");
$query = "SELECT SUM(`credits`) AS `total` FROM `myTable` WHERE `user_id` =2";
$processquery = $mysqli->query($query);
$result = $processquery->fetch_assoc();
echo $result['total'];
Use sum()
SELECT sum(credit) as sum_credit
FROM myTable
WHERE user_id = 2
Use aggregate function SUM():
SELECT SUM(credit) as TotalCredit
FROM myTable
WHERE user_id='2'
Read more about aggregate functions here.
Try SUM() in mysql
SELECT SUM(credit) as totalcredit FROM myTable WHERE user_id ='2'
will return you matched result sum according to condition
select sum(credit) as total from myTable where user_id = '2'
You can do this by summing the total of the credit column. Check out the MySQL reference manual for the official instructions.
SELECT SUM(credit) AS credit_total
FROM myTable
WHERE user_id = 2
You use the AS keyword to assign a name to your column, then in PHP you can access this column as you would any other by name, just using credit_total. E.g:
$query = "SELECT SUM(credit) AS credit_total
FROM myTable
WHERE user_id = 2";
$result = $mysqli->query($query);
$row = $result->fetch_assoc();
echo $row['credit_total']; // Will output 500
i would like to update a table with mySQL where rows got the same name as the name of given ID and increase and decrease their file_version.
The function should restore and older version of the file.
My table looks like this:
ID | name | file_version
1 | book-1 | 1
2 | book-1 | 2
3 | book-1 | 3
4 | book-2 | 1
The function gives the variables $docId and $fileVersion.
First i search for the max. file_version in the table and store it in a variable.
$query =
'SELECT max(file_version) FROM myTable where doc_name in (
SELECT doc_name FROM myTable
WHERE id = '.$docId.');';
$result1 = mysql_query($query);
$row = mysql_fetch_row($result1);
$maxVersion = $row[0];
After that i set the file_version of the desired ID to max.
$query2 = 'SELECT file_version FROM dscQm_docs WHERE id='.$docId.';';
$result2 = mysql_query($query2);
$row = mysql_fetch_row($result2);
$fileVersion = $row[0];
Now comes the tricky part.
I would like to set the file_version of all docs with the same name as the name of the given ID -1 if their file_version is higher than the file_version of the given ID.
Here's my approach:
$query4 = '
UPDATE myTable SET file_version = file_version -1 WHERE(
SELECT * FROM myTable
WHERE name in (
SELECT name FROM myTable
WHERE id = '.$docId.' AND file_version > '.$fileVersion.'
);
';
That doesn't work.
I'm sorry..i'm not good in SQL. I know this is crap.
Could you help me with the last SQL statement?
Thanks in advance.
Try this, you will avoid the much dreaded IN operator with subqueries (they can sometimes be inefficient, to say the least)!
update myTable as t1
left join myTable as t2 on (t1.name = t2. name)
set t2.file_version = t2.file_version -1
where t2.file_version > t1.file_version
and t1.id = *YOUR_ID*
$query4 = '
UPDATE myTable SET file_version = file_version -1 WHERE name like (
SELECT name FROM myTable
WHERE id = '.$docId.' AND file_version > '.$fileVersion.'
)';
Try this?
EDIT: If u know the name, why don't you just compare the name only?
Like:
$query4 = "UPDATE myTable SET file_version = file_version -1 WHERE name like '$name'";
EDIT2: Why don't you work OO? Instead of using so many different queries? To always get 1 value... Then you can store all the data related to 1 row at the same time.
Lets say I have 3 columns and 3 rows, the first column is for ID, the second is names, third is votes. like:
+----+------+-------+
| id | name | votes |
+----+------+-------+
| 1 | bob | 7 |
| 2 | jill | 2 |
| 3 | jake | 9 |
+----+------+-------+
How can I have PHP compare the values in the votes field and sort it by whichever had the highest number, and attach a rank of #1,2,3, etc. depending on how many votes it had?
Each value will be displayed on a separate page. For example, if I went to 'bob's page' with the ID of 1, I would need it to display '#2 bob' since he would be ranked 2nd by votes.
You can make a separate column rank and update it by running the following code whenever your vote changes. This method will make you more efficient as in this you wont be sorting the table again and again when user visits his page:
$q = "select * from tableName order by votes DESC";
$a = mysql_query($q);
$count = 1;
while($arr = mysql_fetch_array($a){
$up = "update tableName set(rank) VALUES($count) WHERE name=$arr['name']";
$aq = mysql_query($up);
$count++;
}
Now on individual pages, you can just retrieve the rank value and show
$user = "Bob";
$q = "select rank from tableName where name=$user";
$a = mysql_query($q);
$arr = mysql_fetch_array($a);
echo $arr[0];
Also this(a slight modification in other answer) should work for you :-
SELECT #rownum:=#rownum+1 AS rank, name, vote FROM table, (SELECT #rownum:=0) as P ORDER BY vote DESC
You could read the values returned by your query into an array and then sort said array.
You want a SELECT query doing an ORDER BY votes, and create a special variable to handle the rank of a row:
SELECT #rownum:=#rownum+1 AS rank, name, vote FROM table, (SELECT #rownum:=0) ORDER BY vote DESC
This query should let you fetch an array with the rank, name, and number of votes of each person of your table.
Alternatively, you can just sort by vote, and add the rank value yourself with PHP.
You can use the MySQL 'ORDER BY' and display those ranks using PHP:
For this example:
<?php
//connection
//DB selection
$query = "SELECT * FROM table_votes ORDER BY votes DESC";
$result = mysql_query($query);
for(int $i=1; $row = mysql_fetch_array($result);i++)
{
echo "#".$i.$row['name']."<br/>";
}
?>