MySql - PHP UPDATE - php

with this code:
$result = mysql_query("
UPDATE skroutz SET
Image_Link=(
SELECT file_url
FROM bonnie_virtuemart_medias
INNER JOIN bonnie_virtuemart_product_medias
ON bonnie_virtuemart_medias. virtuemart_media_id =bonnie_virtuemart_product_medias. virtuemart_media_id
WHERE virtuemart_product_id=Unique_ID LIMIT 1)",$db);
I get a path of the form
/images/stories/virtuemart/product/1200x1000.gif
Get it from table in my database that already exists and the transfer to another table with UPDATE
Trying to add a static string in front of the name but I can not.
Specifically, I want the new table to convey stored as:
http://www.example.com/images/stories/virtuemart/product/1200x1000.gif
That adds to data in front the: www.example.com
Can someone help?
What I tried without success:
$result = mysql_query("
UPDATE skroutz
SET Image_Link=('www.example.com'
SELECT file_url
FROM bonnie_virtuemart_medias
INNER JOIN bonnie_virtuemart_product_medias
ON bonnie_virtuemart_medias. virtuemart_media_id =bonnie_virtuemart_product_medias. virtuemart_media_id
WHERE virtuemart_product_id=Unique_ID LIMIT 1)",$db);

You need to use the CONCAT function:
$result = mysql_query("
UPDATE skroutz
SET Image_Link=(
SELECT CONCAT('www.example.com', file_url)
FROM bonnie_virtuemart_medias
INNER JOIN bonnie_virtuemart_product_medias
ON bonnie_virtuemart_medias. virtuemart_media_id = bonnie_virtuemart_product_medias. virtuemart_media_id
WHERE virtuemart_product_id=Unique_ID LIMIT 1)",$db);

Since I made update of your question it is obviously that you are trying something strange.
If I got your goal correctly you can try something like:
SET Image_Link=CONCAT('www.example.com',(
SELECT file_url
...
))",$db);

Related

How to user SQL two table field.? PHP

Here I want to access two table field but I cant get success. Here is my little code. please check that. I want to access Analysis.Right and tabl3.right.
I am printing its with foreach loop. like $res['Right'] of Analysis and $res['right'] of tabl3. when I try to print this it's show me error
Undefind index Right.
any one can help me
$qry = "select Analysis.Q_Id, tabl3.Q_Id, Analysis.Right, tabl3.right from tabl3 INNER JOIN Analysis ON Analysis.Q_Id = tabl3.Q_Id where Analysis.Q_Id = 3";
please help..
you have tow column with right name related to different table so there is not a column name right but 'Analysis.Right ' or 'tabl3.right'
or you can assign an alias for set the column name equalt to Right where you need .. eg:
$qry = "select
Analysis.Q_Id
, tabl3.Q_Id
, Analysis.Right as Right
, tabl3.right as Right_t3
from tabl3
INNER JOIN Analysis ON Analysis.Q_Id = tabl3.Q_Id where Analysis.Q_Id = 3";
Your result set has columns with the same name. Give them different names:
select t3.Q_Id, a.Right as a_right, t3.right as t3_right
from tabl3 t3 inner join
Analysis a
on a.Q_Id = t3.Q_Id
where a.Q_Id = 3;
When you look for the names in your code, look for a_right and t3_right.
Note that you don't need to return Q_Id twice. The ON clause guarantees that the values are the same.

Left Join between two databases using PDO

I have two databases and I am trying to compare two tables. My code does not seem to be working, not sure what I am doing wrong.
Here is the code.
<?php
include 'connection.php';
/*
* This code compares between two tables
*/
//SQL call
$getData = $connection->prepare("SELECT `CustomerCity` FROM `auth` LEFT JOIN `tb_data.cobs.city` WHERE `CustomerCity` = `tb_data.cobs.city` LIMIT 3");
$getData->execute();
$gotData = $getData->fetchAll(PDO::FETCH_ASSOC);
print_r($gotData);
In my database I have two tables, on is cobs, the other is tb_data. tb_data has a table called cobs and auth is a table within a database called d_data. Both of these tables have a city column. I need get every record in auth that has a city that matches in the cobs table.
That looks like the query is using a mixture of explicit join syntax with obsolescent syntax for the join using the WHERE clause for the join conditions.
If so try:-
SELECT CustomerCity
FROM auth
LEFT JOIN tb_data.cobs
ON auth.CustomerCity = cobs.city
LIMIT 3
Others have pointed out that your query is wrong, but have not provided a correct answer. This is what you are likely looking for:
SELECT `auth.CustomerCity` FROM `auth`
LEFT JOIN `tb_data.cobs` ON `tb_data.cobs.city` = `auth.CustomerCity`
LIMIT 3
Try this :
Select *.auth, *.cobs from auth join cobs on auth.city = cobs.city limit 3
The query is incorrect, you need to specify the link betweeen the tables auth and tb_data.cobs.city. For example:
SELECT
*
FROM
FOOTABLE FOO
LEFT JOIN
BARTABLE BAR ON BAR.FOO_ID FOO.ID = -- here goes the link between them
WHERE
...

Cakephp Update datafield with ENUM

i dont know why i am getting this issue. i have set the datatype to ENUM. when i set its valye 'Y','N'
then its working fine for one field. Now i have another field named type ( ENUM) = '0','1','2'
Now i want to change/update its value then its not working.
here below i have mentioned my query
$BiddersItem = $this->paginate("BiddersItem", array_merge(array("BiddersItem.type"=>"2"), $filter));
that is the piece of the code for the update. i hope you can understand this.
i have set my debug on so check the query
SELECT COUNT(*) AS `count` FROM `event_mang_db`.`bidder_item_master` AS `BiddersItem` LEFT JOIN `event_mang_db`.`item_master` AS `Item` ON (`BiddersItem`.`item_id` = `Item`.`item_id`) LEFT JOIN `event_mang_db`.`atendee_master` AS `Attendee` ON (`BiddersItem`.`atendee_id` = `Attendee`.`atendee_id`) LEFT JOIN `event_mang_db`.`event_master` AS `Event` ON (`BiddersItem`.`event_id` = `Event`.`event_id`) WHERE **`BiddersItem`.`type` = 2** AND `BiddersItem`.`event_id` = 2
issue is that when you set BiddersItem.type` = '2' then its work fine.
suggest me the solution
thanks in advance

How to Update records with field of same records using single query?

I have one user table with column name id,father_email,mother_email,email_notification and many more, now i want to update email_notification column with
father_email,mother_email I did it with multiple queries but i want to know that how to do it with single query only so it saves the execution time.
my code are as follows:
<?php
$qry="SELECT id,CONCAT(father_email,',',mother_email) as notify FROM user";
$query=mysql_query($qry);
while($row=mysql_fetch_assoc($query))
{
$qry2="UPDATE user SET email_notification='".$row['notify']."' WHERE id=".$row['id']."";
mysql_query($qry2);
}
?>
its working fine but i want to know how to do it with single query
This will update all email_notification columns for all users:
UPDATE user
SET email_notification = CONCAT_WS(',', father_email, mother_email)
(i think it's better to use CONCAT_WS that will skip null values in father_email or in mother_email)
This will replace all of them
UPDATE user AS u
INNER JOIN user AS ul ON ul.id = u.id
SET u.email_notification = CONCAT(ul.father_email,',',ul.mother_email)
UPDATE user
SET email_notification = CONCAT_WS(',', father_email, mother_email)

mysql php join statement

I am not sure if that join statement is writen in the right way:
<?php
function generateComment($commentID)
{
$avatar_Q=mysql_query("
SELECT * FROM comments com
INNER JOIN users us ON com.user_id=us.user_id
WHERE comment_id=$commentID // will that $commentID be red in that query string or will it treat it as a string "commentID"
");
if($row=mysql_fetch_array($avatar_Q))
{
$userName=$row["us.user_name"]; // do I refer to the fields like that
$avatarPath=$row["us.avatar"];
$avatarRep=$row["us.reputation"];
$message=$row["com.comment"];
$date=$row["com.date"];
}
mysql_close();
if(!isset($avatarPath))
{
$avatarPath="blank picture";
}
?>
Is this the most efficient way to write a join statement
Your query is written correctly, but you can improve it by specifing the table on comment_id, and for a better returning I suggest you to specify with column you want back, also I will use the variable outside the "string", because it's a numeric value (I suppose you omit the single quote like...
$sql = "Select com.command_it, etc
FROM ..... WHERE com.comment_id = ".$commentID."";
If otherwise the com.comment_id is text or varchar you must use the single quote like:
$sql = "Select com.command_it, etc
FROM ..... WHERE com.comment_id = '".$commentID."'";
Moreover this way you get only the rows where there is a user and a comment, if one user have no comment you don't retrieve that user...
If you want the user also if he has no comments you must use a LEFT JOIN
$sql = "SELECT com.comment_id, etc FROM users us
LEFT JOIN comments com ON com.user_id=us.user_id
WHERE com.comment_id=".$commentID."";
Not sure what you're doing with this line
WHERE comment_id=$commentID // will that $commentID be red in that query string or will it treat it as a string "commentID"
");
But you need to specify which table comment_id is coming from, say comments, then you might do something like
$avatar_Q=mysql_query("SELECT * FROM comments com
INNER JOIN users us ON com.user_id=us.user_id
WHERE com.comment_id=$commentID");
Not sur if it's the best way, but you could try to mysqlslap a mysql DB with it. Compare it with left join and other types of join and see which one is the best in your case. MySQL is all about slapping.

Categories