Hello I have this problem and can't find a solution:
I have 4 tables with same structure for example as follows:
Table 1: Result
Table 2: Store 1
Table 3: Store 2
Table 4: Store 3
Tables fields: ID - Code - Name - Value
I need a query to read the "Value" for each specific record from tables (Store 1 - Store 2 - Store 3) and calculate the average and save it in table (Result)...
and go on for the next record until it's done.
Note: I'm using PHP and MySQL...
Thanks in advanced...
SELECT
result.id,
result.`code`,
result.`name`,
result.value,
term1.value,
term2.value,
term3.value
FROM result
INNER JOIN store1 ON result.`code` = store1.`code`
INNER JOIN store2 ON result.`code` = store2.`code`
INNER JOIN store3 ON result.`code` = store3.`code`
WHERE result.`code` = 123456
ORDER BY result.serial ASC
The average is just the sum of the values divided by the number of values (3), this is grade school arithmetic.
UPDATE result AS r
JOIN store1 AS s1 ON s1.code = r.code
JOIN store2 AS s2 ON s2.code = r.code
JOIN store3 AS s3 ON s3.code = r.code
SET r.value = (s1.value+s2.value+s3.value)/3
To do lots of columns, you can generate the SQL in PHP:
$cols = array('col1', 'col2', 'col3', ...);
$sets = implode(', ', array_map(function($col) {
return "r.$col = (s1.$col + s2.$col + s3.$col)/3";
}, $cols));
$sql = "UPDATE result AS r
JOIN store1 AS s1 ON s1.code = r.code
JOIN store2 AS s2 ON s2.code = r.code
JOIN store3 AS s3 ON s3.code = r.code
SET $sets";
If you're using PHP before 5.3.0, you can define a named function to call it with array_map
function make_assignment($col) {
return "r.$col = (s1.$col + s2.$col + s3.$col)/3";
}
$sets = implode(', ', array_map('make_assignment', $cols));
You can create views in MySQL for this. Then there will not need any for this. View is a virtual table. On every change in any table it will automatically updated. You don't need any query
create view result as select t1.code as code,(t1.value+t2.value+t3.value)/3 as value from `testcake`.`test1` as t1 JOIN `testcake`.`test2` as t2 ON t1.code = t2.code JOIN `testcake`.`test3` as t3 ON t1.code = t3.code
Related
I'm looking for a way to update the data in 2 tables with an inner join, but it doesn't work for me.
What I want is to update the data of the dbo.UserInfo table and at the same time the Coin field of the dbo.UserInfovariable table.
UserInfo table
UserIndex
UserID
UserPassword
Nickname
1
admin
123456
UserPHP
UserInfoVariable table
UserIndex
Experience
Coin
1
989981454
§8000
I have tried with this sentence, it only works with "a", when I add b.Coin the sentence stops working
$sql = "UPDATE a
SET a.UserID = '$data[1]',
a.UserPassword = '$data[2]',
a.NickName = '$data[3]' ,
b.Coin ='$data[4]'
FROM UserInfo as a
INNER JOIN UserInfoVariable b ON a.UserIndex = b.UserIndex
where a.UserIndex = '$data[0]'";
I have two tables,
#table 1 contains the following data structure
NO category_name category_code amount type
1 provident_fund PF 1000 Deduction
2 Home Allowance HM 12000 Earning
3 Basic_pay BP 35000 Earning
#table 2 contains the following data structure
NO group_name payroll_cat_name
1 permanent PF,HM,BP,TX
1 visiting HM,BP
from the table 2, I can get the value of payroll_cat_name using the query
$a = "Select payroll_cat_name from table_2";
using the above query, I'm getting the value PF,HM,BP,TX.
Now, I have to use this multiple values PF,HM,BP,TX in order to get the sum amount from table 1.
Following is my code that I have tried,
include "../db.php";
$a = "Select payroll_cat_name from table_2";
$b = mysqli_query($con,$a);
$c = mysqli_fetch_array($b);
$d = $c["payroll_cat_name"];
echo "$d";
$myArray = explode(',', $d);
print_r($myArray);
$tr = "select SUM(amount) as am from table_1 where category_code in ($d)";
$rt = mysqli_query($con,$tr);
$new = mysqli_fetch_array($rt);
$gh = $new["am"];
That’s a poor data model. There should be a separate table to store the relation between groups and categories, which each tuple on a different row. The linked commented by sticky bit on the question gives great explanation on how and why.
For your current structure, one option using uses find_in_set() and a subquery:
select t2.*,
(
select sum(t1.amount)
from table_1 t1
where find_in_set(t1.category_code, t2.payroll_cat_name)
) as t1_amount
from table_t2 t2
I have 3 tables:
place (placeId,placeName,...)
event (eventId, eventName,...)
picture (pictureId,associateId,picturePath,type [1 place 2 event])
This is what I want:
When picture type is 1 picture.associateId=place.placeId return path
when picture type is 2 picture.associateId=event.eventId return path
1 place -> many events
1 place -> one picture
1 event -> one picture
May be something like this (please help):
SELECT Place.placeName, Picture.picturePath, Event.eventId, ... FROM Place INNER JOIN Event ON Place.placeId = Event.eventPlace INNER JOIN Picture ON (IF Picture.type=1 return Picture.picturePath ELSE Picture.type=2 return Picture.picturePath)
This consult return me a json to show events on a page deppending of the date:
SELECT Place.placeName, Place.placePopularity, Picture.picturePath, Event.eventId, Event.eventCount
FROM Place INNER JOIN Picture ON Place.placeId = Picture.associateId
INNER JOIN Event ON Place.placeId = Event.eventPlace
WHERE Picture.type = 2 AND Event.eventDate = '$date'
ORDER BY Event.eventCount DESC
Run an if else with this
if () { // picture type 1
// this is the query
$query = "SELECT pictureTable.picture_path FROM placeTable INNER JOIN picture_table ON placeTable.placeId = picture_table.associateId";
} else { // picture type 2 means for event
$query = "SELECT pictureTable.picture_path FROM eventTable INNER JOIN picture_table ON eventTable.eventId = picture_table.associateId";
// now you can get the result
}
Hope this helps.
I have 2 tables.
Table A: trades: which contains the columns: tradeID, tradeName, tradeShow, and tradeGuy.
Table B: offers: which contains the columns: tradeID, offerName, offerGuy.
I'm trying to select all columns from table A (trades) WHERE the value of "tradeShow" = 'Yes', And the value of "tradeGuy" != the user's Username. That much is easy, but I also don't want to select any records which have an offer created by the user. In other words, in table B (offers), offerGuy != Username WHERE trade ID from Table B = tradeID from Table A.
But, how do I merge these 2 conditions? I've tried this:
$sql = "SELECT *
FROM trades t1
JOIN offers t2
ON (t1.tradeID = t2.tradeID)
WHERE t1.tradeShow='Yes' AND t1.tradeGuy!='$username' AND t2.offeringGuy!='$username'";
But the problem with that is it only selects the records from trades which have an offer, because of the forth line: ON (t1.tradeID = t2.tradeID), as in it only selects trades which have a record in (offers) that mentions their tradeID.
I've also tried an awkward attempt to link the 2 tables with a meaningless link by adding a "linker" column to each table with the default value of "XXX", and did this:
$sql = "SELECT *
FROM trades t1
JOIN offers t2
ON (t1.linkerA = t2.linkerB)
WHERE t1.tradeShow='Yes' AND t1.tradeGuy!='$username' AND (t2.offeringGuy!='$username' WHERE t1.tradeID=t2.tradeID)";
But the problem with that is using 2 Where clauses...
So, how do I merge the 2 conditions?
What you're looking for is called an OUTER JOIN (in this case a LEFT OUTER JOIN) which will give you null results for missing matches, something like;
SELECT *
FROM trades t1
LEFT OUTER JOIN offers t2
ON t1.tradeID = t2.tradeID AND t2.offeringGuy = '$username'
WHERE t1.tradeShow='Yes' AND t1.tradeGuy!='$username' AND t2.offeringGuy IS NULL
We add a condition to the LEFT JOIN that we're only interested in matches against t2.offeringGuy = '$username', which will return NULL values in t2's fields if there is no match.
Then we just check that t2.offeringGuy IS NULL to find the non matches.
I would do this with not exists rather than an explicit join:
SELECT *
FROM trades t
WHERE t.tradeShow = 'Yes' AND t.tradeGuy <> '$username' and
not exists (select 1
from offers o
where t.tradeID = o.tradeID and o.tradeGuy = '$username'
);
I've been working on a project that until now has only needed to find 1 row from the joined table. But now I need to grab multiple rows..
So as it stand my sql works something like:
Select rows for each company for this particular project which alone would find company details (name, id, telephone.. blah).
Then I join a table that contains form data submitted for each company (multiple forms - so multiple records)
Until now i have been specifying one formid to look for in the join, but now i need to specify multiple ones.
If I use WHERE form_id = 1 OR form_id = 2 OR form_id = 3 ... I get a result of only the first form match that is found per company..
If I mix up the query so it looks for the forms 1st and returns multiple records for each company with different form data - that works in this sense..
But I am then looping through this array in a view and creating a table row per record (previously each row was a new company) but using the latter would cause multiple records to show for the same company.
Any way I can do this? I tried group by with the latter method but this results in only 1 record again.
SELECT DISTINCT p.project_company_has_user_id, p.project_company_has_user_project_id, p.project_company_has_user_user_id, c.company_id, c.company_hall_no, c.company_company_name, c.company_type, c.company_country, c.company_stand_number, c.company_image_file_1, p2.project_id, p2.project_name, u.user_id, u.user_username, o.orders_id, o2.order_detail_id, o2.order_detail_product_id, f2.form_question_has_answer_id, f2.form_question_has_answer_request, f2.form_question_has_answer_form_id, f2.form_question_has_answer_user_id
FROM project_company_has_user p
INNER JOIN company c ON p.project_company_has_user_company_id = c.company_id
INNER JOIN project p2 ON p.project_company_has_user_project_id = p2.project_id
INNER JOIN user u ON p.project_company_has_user_user_id = u.user_id
INNER JOIN form f ON p.project_company_has_user_project_id = f.form_project_id
LEFT JOIN orders o ON p.project_company_has_user_user_id = o.orders_user_id
LEFT JOIN order_detail o2 ON ((o2.order_detail_orders_id = o.orders_id AND (o2.order_detail_product_id = 65 OR o2.order_detail_product_id = 68 OR o2.order_detail_product_id = 64)))
LEFT JOIN form_question_has_answer f2 ON ((f2.form_question_has_answer_form_id = 297 AND f2.form_question_has_answer_user_id = p.project_company_has_user_user_id))
WHERE (f.form_template_name = "custom" AND p.project_company_has_user_garbage_collection = 0 AND p.project_company_has_user_project_id = 48) AND (LCASE(c.company_country) LIKE "%uk%" OR LCASE(c.company_country) LIKE "%uk%") ORDER BY company_company_name asc
you need another field in order_detail as o2 . this field is row_index(position),etc for positioning record
LEFT JOIN order_detail o2 ON (o2.row_index=1 AND (o2.order_detail_orders_id = o.orders_id AND (o2.order_detail_product_id = 65 OR o2.order_detail_product_id = 68 OR o2.order_detail_product_id = 64)))
Personally I would use an Outer Join for the table of which elements you need to list all matches. Should you them need to clean up that data you can build the logic into the Join Condition (as step 2). Depending on the volume of data you are handling and whether or not you need to reuse it later in the same proc, you may want to post that primary dataset into a temp table and use that as source (primary) for your later logic.
Hope that helps. If you need the code, let me know, but it is pretty straight forward.
Regards
Mac