I want to update the values of one column in a table from '0' to '1', if either of four values in columns in another table are '1'. Somehow this doesn't seem to work and I was just wondering if anyone could help me get the code right or find a different way of doing it if it's not possible,
mysql_query("UPDATE members
INNER JOIN forum_banners ON members.id = forum_banners.userid
SET members.beta = '1' WHERE forum_banners.bebeta = '1' OR
forum_banners.bibeta = '1' OR forum_banners.cbeta = '1' OR
forum_banners.wbeta = '1'") or die(mysql_error());
That's what I tried, but it's not working, I suspect because of the OR. I tried having all updatings in different mysql_query bits, but that didn't work either.
You should be able to update from multiple table references. This is untested, but gives you an idea:
UPDATE
members, forum_banners
SET
members.beta = '1'
WHERE
members.id = forum_banners.userid
AND forum_banners.bebeta = '1'
OR forum_banners.bibeta = '1'
OR forum_banners.cbeta = '1'
OR forum_banners.wbeta = '1'
http://dev.mysql.com/doc/refman/5.0/en/update.html
Note "Multi-Table syntax"
Try
UPDATE
m
SET
m.beta = '1'
FROM
members m
INNER JOIN
forum_banners fb
ON m.id = fb.userid
WHERE
fb.bebeta = '1'
OR fb.bibeta = '1'
OR fb.cbeta = '1'
OR fb.wbeta = '1'"
Aliases also help make your syntax a little neater.
Related
i want to update field in my database.
I have two table and table field like bellow
Table operations_per_assembly Field operation_id,is_mecahnical
Table operations Field id,repair_type_id
Now i want to update is_mechanical field where repair_type_id = 3
My query
UPDATE
`operations_per_assembly`
JOIN `operations`
ON `operations`.`id` = `operations_per_assembly`.`operation_id`
SET `operations_per_assembly`.`is_mechanical` = '4'
WHERE `operations_per_assembly`.`operation_id` = `operations`.`id`
AND `operations_per_assembly`.repair_type_id = 3
Please help me.
Put the repair_type_id = 3 condition in the join conditions. This way you are telling to join only on repair_type_id = 3 so you will only get those records.
UPDATE
`operations_per_assembly`
JOIN `operations`
ON `operations`.`id` = `operations_per_assembly`.`operation_id` AND `operations`.repair_type_id = 3
SET `operations_per_assembly`.`is_mechanical` = '4'
UPDATE `operations_per_assembly` a
JOIN `operations` b
ON a.operation_id = b.id
SET a.is_mechanical = '4'
WHERE codition (user proper condition)
In the WHERE clause you are using operations_per_assembly.repair_type_id but your operations_per_assembly table does not have repair_type_id in it.
So try the below query:
UPDATE `operations_per_assembly` PAS
JOIN `operations` OPE ON OPE.`id` = PAS.`operation_id`
SET PAS.`is_mechanical` = '4'
WHERE OPE.repair_type_id = 3
How can I ensure that s.user_id and w.stage_id do not have duplicate rows? You can see in the screengrab that user_id 117 occurs in two rows. I want to prevent this - perhaps by only including the latest submission in the query?
My returned data is as follows:
My query:
SELECT DISTINCT r.breakout_id, r.case_id, r.stage_id, r.chart_id, s.stage1_choice,s.user_id,w.stage1,w.user_id
FROM Results as r, Submissions as s, Switch as w
WHERE r.breakout_id = '1' AND s.breakout_id = '1' AND w.breakout_id = '1' AND s.user_id = w.user_id AND w.case_id = r.case_id AND s.case_id = r.case_id AND s.stage1_choice IS NOT NULL ORDER BY w.user_id
Try this:
SELECT r.breakout_id, r.case_id, r.stage_id, r.chart_id,
s.stage1_choice,s.user_id,max(w.stage1),w.user_id
FROM Results as r, Submissions as s, Switch as w
WHERE r.breakout_id = '1' AND s.breakout_id = '1' AND w.breakout_id = '1' AND s.user_id = w.user_id
AND w.case_id = r.case_id AND s.case_id = r.case_id AND
s.stage1_choice IS NOT NULL
GROUP BY r.breakout_id, r.case_id, r.stage_id, r.chart_id,
s.stage1_choice,s.user_id,w.user_id
ORDER BY w.user_id;
i'm in the process of joining two tables together under two different conditions. For primary example, lets say I have the following nested query:
$Query = $DB->prepare("SELECT ID, Name FROM modifications
WHERE TYPE =1 & WFAbility = '0'");
$Query->execute();
$Query->bind_result($Mod_ID,$Mod_Name);
and this query:
$Query= $DB->prepare("SELECT `ModID` from `wfabilities` WHERE `WFID`=?");
$Query->bind_param();
$Query->execute();
$Query->bind_result();
while ($Query->fetch()){ }
Basically, I want to select all the elements where type is equal to one and Ability is equal to 0, this is to be selected from the modifications table.
I further need to select all the IDs from wfabilities, but transform them into the names located in modifications where WFID is equal to the results from another query.
Here is my current semi-working code.
$Get_ID = $DB->prepare("SELECT ID FROM warframes WHERE Name=?");
$Get_ID->bind_param('s',$_GET['Frame']);
$Get_ID->execute();
$Get_ID->bind_result($FrameID);
$Get_ID->fetch();
$Get_ID->close();
echo $FrameID;
$WF_Abilties = $DB->prepare("SELECT ModID FROM `wfabilities` WHERE WFID=?");
$WF_Abilties->bind_param('i',$FrameID);
$WF_Abilties->execute();
$WF_Abilties->bind_result($ModID);
$Mod_IDArr = array();
while ($WF_Abilties->fetch()){
$Mod_IDArr[] = $ModID;
}
print_r($Mod_IDArr);
$Ability_Name = array();
foreach ($Mod_IDArr AS $AbilityMods){
$WF_AbName = $DB->prepare("SELECT `Name` FROM `modifications` WHERE ID=?");
$WF_AbName->bind_param('i',$AbilityMods);
$WF_AbName->execute();
$WF_AbName->bind_result($Mod_Name);
$WF_AbName->fetch();
$Ability_Name[] = $Mod_Name;
}
print_r($Ability_Name);
See below:
SELECT ModID,
ID,
Name
FROM modifications M
LEFT JOIN wfabilities WF
ON WF.ModID = M.ID
WHERE TYPE =1 & WFAbility = '0'
To do this, you need to join your tables, I'm not quite sure what you are trying to do so you might have to give me more info, but here is my guess.
SELECT ID, Name, ModID
FROM modifications
JOIN wfabilities
ON WFID = ID
WHERE TYPE = '1'
AND WFAbility = '0'
In this version I am connecting the tables when WFID is equal if ID. You will have to tell me exactly what is supposed to be hooking to what in your requirements.
To learn more about joins and what they do, check this page out: MySQL Join
Edit:
After looking at your larger structure, I can see that you can do this:
SELECT modifications.Name FROM modifications
JOIN wfabilities on wfabilities.ModID = modifications.ID
JOIN warframes on warframes.ID = wfabilities.WFID
WHERE warframes.Name = 'the name you want'
This query will get you an array of the ability_names from the warframes name.
This is the query:
"SELECT A.ID, A.Name,B.ModID,C.Name
FROM modifications as A
LEFT JOIN wfabilities as B ON A.ID = B.WFID
LEFT JOIN warframes as C ON C.ID = B.WFID
WHERE A.TYPE =1 AND A.WFAbility = '0' AND C.Name = ?"
I know I've done this before, but haven't done much SQL in a while.
I have a table called lead_detail, it has the following columns: id, lead_id, form_id, field_number, value.
I'm trying to get results and then filter them.
So, originally, I got all the lead ids that had a particular answer for a particular question.
Let's say the first filter was that I wanted all results for an answer of "yes" to a question of "do you like chicken" (field_number of 4).
$leadIds = query(SELECT lead_id FROM lead_detail WHERE field_number = '4' AND value = 'yes' AND form_id = '1')
$leadIds = implode(', ', $leadIds);
Then, I could get the appropriate filtered data by
SELECT * FROM lead_detail WHERE form_id = '1' and lead_id IN ($leadIds)
This works perfectly.
If I want to add a SECOND filter though, I need to do two queries to the same columns in the same query. I'm thinking I need to do some kind of join with table aliases, or some sub selects, but can't remember how to do it.
So if I needed to know who answered "yes" to a question of "do you like chicken" AND who answered "18-24" on "what is your age", how would I get the appropriate $leadIds?
It'd be something like the combination of
SELECT lead_id FROM lead_detail WHERE field_number = '4' AND value = 'yes' AND form_id = '1'
AND
SELECT lead_id FROM lead_detail WHERE field_number = '5' AND value = '18-24' AND form_id = '1'
An inner join would work for this:
SELECT DISTINCT(ld1.lead_id)
FROM lead_detail ld1
INNER JOIN lead_detail ld2 ON (ld1.lead_id = ld2.lead_id)
WHERE ld1.field_number = '4' AND ld1.value = 'yes' AND ld1.form_id = '1'
AND ld2.field_number = '5' AND ld2.value = '18-24' AND ld2.form_id = '1'
There is possibly a neater solution however.
Couldn't you just use
SELECT lead_id
FROM lead_detail
WHERE ((field_number = '4' AND value = 'yes') OR (field_number = '5' AND value = '18-24')) AND form_id = '1'
I have googled my problem but didnt get the answer.
I want to list all of the results of below sql including NULL (when COUNT(review.id) return 0 also) but instead i just got the results of articles of place that only contains review.
$sql = "SELECT tbl_place.id, tbl_place.region_id, tbl_place.subregion_id, tbl_place.title, tbl_place.metalink, tbl_place.img_thumbnail, tbl_place.summary, tbl_place.category1_id, tbl_place.category2_id, tbl_place.category3_id, COUNT(review.id) AS total_review FROM tbl_place
JOIN review ON tbl_place.id = review.place_id
WHERE
tbl_place.category1_id = '32' AND
tbl_place.status = '1' AND
review.rating != '0.00'
GROUP BY tbl_place.id
ORDER BY total_review $by
LIMIT $limit OFFSET $offset";
please use left join for review table instead of join. join is by default inner join so it will take only matched records.
the sql should be :
$sql = "SELECT tbl_place.id,
tbl_place.region_id,
tbl_place.subregion_id,
tbl_place.title,
tbl_place.metalink,
tbl_place.img_thumbnail,
tbl_place.summary,
tbl_place.category1_id,
tbl_place.category2_id,
tbl_place.category3_id,
(SELECT COUNT(*) FROM review WHERE review.rating != '0.00' AND tbl_place.id = review.place_id ) AS total_review
FROM tbl_place WHERE
tbl_place.category1_id = '32' AND
tbl_place.status = '1'
GROUP BY tbl_place.id
ORDER BY total_review $by";
it's working! thx guys!