I need alternative syntax for my sql, to deduct the qty from another table to my raw material table
$sql3 = "UPDATE material_inventory
join product_check
ON material_inventory.qty = material_inventory.qty
SET material_inventory.qty = material_inventory.qty - product_check.qty
WHERE product_check.pc_id = '$id'
AND product_check.date2 = '$date'
LIMIT 1";
$result3 = mysqli_query($conn,$sql3);
It looks like limit doesn't work in join update with join clause.
anyway thanks
Looks like your ON condition is wrong. Until you add schema of relevant tables, all I can suggest from given scenario is following query:
$sql3 = "UPDATE material_inventory
join product_check
ON material_inventory.qty = product_check.qty //modified line
SET material_inventory.qty = material_inventory.qty - product_check.qty
WHERE product_check.pc_id = '$id'
AND product_check.date2 = '$date'
LIMIT 1";
$result3 = mysqli_query($conn,$sql3);
If the above query doesn't get the desired output, please add schema so that I can provide more accurate solution.
Related
Here is my simple query:
$sql = "SELECT * FROM donations Order By userid";
$result = $mysqli->query($sql);
while($row = $result->fetch_assoc()){
$json[] = $row;
}
$data['data'] = $json;
I use it to display all data from the 'donations' in a table. Fields are: userid,date,amount.
In that same table, I'd like to add firstname and lastname of corresponding userid which are stored in mymembers table. The condition should be WHERE donations.userid = mymembers.id.
I need help adding that condition for every row resulting from the $sql query.
Use join and change query to
SELECT * FROM donations
INNER JOIN mymembers on (donations.userid = mymembers.id)
Order By donations.userid
I need to get 3 tables's values , from first I need to get aff_id where v_id = 5 , from second I need to get user id where aff_id = first's aff_id , and from third I need to get username , email , id where id = second's aff_id . I can't write correct mysql query to get data , please , help me to get it . Here is my wrong code
SELECT * FROM wp_vendor_affiliates WHERE vendor_id = 21 LEFT JOIN wp_affiliate_wp_affiliates
SELECT wp_vendor_affiliates.affiliate_id ,
wp_affiliate_wp_affiliates.user_id
FROM wp_vendor_affiliates
INNER JOIN wp_affiliate_wp_affiliates INNER JOIN
Please , hetp me , and correct my query . Thanks fot helping and for support
Use this code
$id1 = 5;
$query1 = "SELECT * FROM table1 WHERE v_id='{$id1}'";
$result1 = mysqli_query($con,$query1);
$row1 = mysqli_fetch_array($result1);
$id2 = $row1['aff_id'];
$query2 = "SELECT * FROM table2 WHERE aff_id='{$id2}'";
$result2 = mysqli_query($con,$query2);
$row2 = mysqli_fetch_array($result2);
$id3 = $row2['aff_id'];
$query3 = "SELECT * FROM table3 WHERE id='{$id3}'";
$result3 = mysqli_query($con,$query3);
$row3 = mysqli_fetch_array($result3);
$id2 = $row3['aff_id'];
Here $con is the connection to your database
$con= mysqli_connect("localhost","root","","data_base");
I used here connection to the localhost. which you'd have to change most certainly. It's a bit long but basic
How can I do something like this:
$query = "SELECT a,b FROM c ORDER BY a";
$query1 = "SELECT a,b FROM '".$query."' WHERE a='".$number."'";
Thank you very much
REAL CASE
$query2 = "SELECT numero,spartenza,sarrivo,opartenza,oarrivo FROM treni ORDER BY opartenza";
$query1 = "SELECT spartenza,sarrivo,opartenza,oarrivo,TIMEDIFF(oarrivo,opartenza) FROM (".$query2.") AS 'ordinata' WHERE numero = '".$id_treno."' ORDER BY opartenza";
Wrap it in parenthesis:
$query1 = "SELECT a,b FROM (".$query.") AS `alias` WHERE a='".$number."'";
Subqueries like this need to be aliased.
MySQl Subquery Documentation
REAL CASE
$query = "SELECT spartenza,sarrivo,opartenza,oarrivo,TIMEDIFF(oarrivo,opartenza) FROM treni WHERE numero = '".$id_treno."' ORDER BY opartenza";
You do not need a subquery at all for this. You can ORDER BY a column that you aren't selecting. One suggestion though would be to alias your TIMEDIFF function like this sothat it will be easier to retrieve.
$query = "SELECT spartenza,sarrivo,opartenza,oarrivo,TIMEDIFF(oarrivo,opartenza) AS `timediff_alias` FROM treni WHERE numero = '".$id_treno."' ORDER BY opartenza";
I was thinking of accomplishing the following as a PHP multi_query. But I'm trying to figure out how to pass the column value from the select query to the insert and update queries.
$query = "SELECT tbl_links.link, link_id
FROM tbl_links
INNER JOIN tbl_items ON tbl_links.item_id = tbl_items.item_id
WHERE tbl_items.item_name like '".$items_name[$counter]."'
AND NOT EXISTS (
select link_id
from tbl_clickedlinks
where tbl_clickedlinks.link_id = tbl_links.link_id
AND tbl_clickedlinks.cust_id = '$items_custID[$counter]'
)
limit 0, 1;" ;
$query .= "INSERT INTO tbl_claimedlinks (cust_id, link_id, claim_time) VALUES ('$items_custID', $row['link_id'], NOW()) ;";
$query .= "UPDATE tbl_links SET click_count = click_count+1 where link_id = '$linkID' ;";*/
Problem is, I'm not sure how to pass the link_id value to the other queries. So I'm thinking I might have to rearrange the queries into one, but again, I'm just not sure how to pull that off.
Anyone got any suggestions?
You need to execute select query 1st then use its output to execute 2nd & 3rd query.
$query = "SELECT tbl_links.link, link_id
FROM tbl_links
INNER JOIN tbl_items ON tbl_links.item_id = tbl_items.item_id
WHERE tbl_items.item_name like '".$items_name[$counter]."'
AND NOT EXISTS (
select link_id
from tbl_clickedlinks
where tbl_clickedlinks.link_id = tbl_links.link_id
AND tbl_clickedlinks.cust_id = '$items_custID[$counter]'
)
limit 0, 1;" ;
$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {
$query2 = "INSERT INTO tbl_claimedlinks (cust_id, link_id, claim_time) VALUES ('$items_custID', $row['link_id'], NOW()) ;";
$query3 = "UPDATE tbl_links SET click_count = click_count+1 where link_id = '$linkID' ;";*/
mysql_query($query2);
mysql_query($query3);
}
I have following script:
$sql = "SELECT * FROM `users`"
$q = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($q);
$sql1 = "SELECT * FROM `other_table`";
$q1 = mysql_query($sql1) or die(mysql_error());
$row1 = mysql_fetch_array($q1);
$item = $row1[$row['username']];
How can I set one variable row inside another, since it does not work. Basically, I need to select username, and then select column with user username from other table, in which is written user points.
I was thinking about adding:
$sql = "SELECT * FROM `users`"
$q = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($q);
$sql1 = "SELECT `".$row['username']."` FROM `other_table` WHERE `uid` = 1";
$q1 = mysql_query($sql1) or die(mysql_error());
$row1 = mysql_fetch_array($q1);
$item = $row1[xxxxxxxxxx]; // DONT KNOW HOW TO DEFINE IT, so it takes out found variable (there is only one).
Guess you want something like
SELECT * FROM table1 t1, table2 t2 WHERE t1.user_name = t2.user_name?
Think about using JOIN
$sql = "SELECT * FROM users;";
$q = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($q);
$sql1 = sprintf("SELECT * FROM other_table where username='%s';", $row['username']);
$q1 = mysql_query($sql1) or die(mysql_error());
$row1 = mysql_fetch_array($q1);
// now $row1 contains the tuple of this user and could access the variables are you would
// normally do e.g. $row1['ID']
SELECT * FROM users AS u INNER JOIN other_table AS o ON u.username = o.username
I'm assuming you want to do this because you want to be able to access all the rows from either table where a particular user name is the same (e.g. the data from users where username="john" and the data from other_table where username="john" for all usernames). No need to nest a result set to do this, just use a JOIN statement and then you can access all the columns as if it was a single result set (because it is):
$sql = "SELECT * FROM users AS u INNER JOIN other_table AS o ON u.username = o.username";
$q = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($q);
$item = $row['any_column'];
FYI you should list out the column you want to retrieve instead of using *, even if you want to retrieve them all, as it is better practice in case you add new columns in the future.