I´m trying to update a third table with the result of a select query - I know how to do this on mysql but access makes insane :)
I have the following statement:
UPDATE ceilings LEFT JOIN
(SELECT ceiling_number AS cc, Count(type=3) AS st FROM ceiling INNER JOIN materials ON ceiling.material_number = materials.id GROUP BY ceiling.ceiling_number) AS q
ON q.cc=ceilings.ceilingid SET ceilings.stairs = q.st;
but it does not update tables and when I want to execute the statement i get the error:
"Operation must use an updateable query"
Would be great if someone could help me with this.
Update queries won't work with several types of other queries, including things like Group by, Totals, CrossTabs, or queries with left or right joins (in combination with inner joins).
There are a couple of solutions around this problem depending on your comfort level with Access and VBA.
Create a temporary table first. Write the results of your query to the temporary table, then use the temporary table to update the results of your ceilings table.
Use VBA to create and store the values of the non-update-able query, then using recordsets, loop thru your records and update the values in your ceilings table.
Related
I am building a query and I need to select from the log table multiple columns, my issue is that i'm trying to find a way to select a column that has an FK in a table that has an FK to another table.
I have:
log.number_id,
numbers.number_id
numbers.country_id,
countries.country_id
Query is almost done, my only issue is that I need to show countries.country_id through an intermediary table FK numbers.country_id, I believe this is an INNER JOIN yet I have no idea how to create the concatenation, I searched google for this, yet I couldn't find something like a general formula of how to execute such an intermediary join.
I'm guessing you're looking for something like this.
Basically joining the table with both id's to the other tables on the common id.
SELECT log.*, ctry.*
FROM numbers AS ctrylog
JOIN log
ON log.number_id = ctrylog.number_id
JOIN countries AS ctry
ON ctry.country_id = ctrylog.country_id
I am working in mysql with queries, but I am new to this. I am joining 5 tables where each table has an identifier and one table is the master. Each related table may have more than one associated record to the master table. I am attempting to join these tables but I can't seem to get rid of the duplicated data.
I want all of the related records to be displayed, but I don't want the data in the master table to display for all results in the related tables. I have tried so many different methods but nothing has worked. Currently I have 4 queries that work for the separate tables, but I have not successfully joined them to have the results display the multiple records in the related table but just one record from the master table.
Here are my individual queries that work:
SELECT
GovernmaxAdditionsExtract.AdditionDescr,
GovernmaxAdditionsExtract.BaseArea,
GovernmaxAdditionsExtract.Value
FROM
GovernmaxExtract
INNER JOIN GovernmaxAdditionsExtract
ON GovernmaxExtract.mpropertyNumber = GovernmaxAdditionsExtract.PropertyNumber
WHERE (((GovernmaxExtract.mpropertyNumber)="xxx-xxx-xx-xxx"));
SELECT
GovernmaxExtract.mpropertyNumber,
GovernmaxDwellingExtract.CardNumber,
GovernmaxDwellingExtract.MainBuildingType,
GovernmaxDwellingExtract.BaseArea
FROM
GovernmaxExtract INNER JOIN
GovernmaxDwellingExtract ON GovernmaxExtract.mpropertyNumber = GovernmaxDwellingExtract.PropertyNumber
WHERE (((GovernmaxExtract.mpropertyNumber)="xxx-xxx-xx-xxx"));
Using these sub queries, I tried to put together 2 of the tables, but now I am getting all records back and it is not reading my input parameter:
SELECT GE.mpropertynumber
FROM
GovernmaxExtract AS GE,
(SELECT
GovernmaxAdditionsExtract.AdditionDescr,
GovernmaxAdditionsExtract.BaseArea,
GovernmaxAdditionsExtract.Value
FROM GovernmaxExtract INNER JOIN
GovernmaxAdditionsExtract ON
governmaxextract.mpropertyNumber = GovernmaxAdditionsExtract.PropertyNumber) AS AE
WHERE GE.mpropertynumber = 'xxx-xxx-xx-xxx'
I tried nested queries, lots of different joins, and I am just not able to wrap my head around this. I am pretty sure I want to do a nested query since I want the main data from the Governmax table to display once with the main data and all records with all info for the associated tables. Maybe I am going about it all wrong.
Our original code was:
SELECT
ge.*,
gde.*,
gfe.*,
gae.*,
goie.*
FROM governmaxextract AS ge
LEFT JOIN governmaxdwellingextract AS gde
ON ge.mpropertyNumber = gde.PropertyNumber
LEFT JOIN governmaxfeaturesextract AS gfe
ON gde.PropertyNumber = gfe.PropertyNumber
LEFT JOIN governmaxadditionsextract AS gae
ON gde.PropertyNumber = gae.PropertyNumber
RIGHT JOIN governmaxotherimprovementsextract AS goie
ON gde.PropertyNumber = goie.PropertyNumber
WHERE ge.mpropertyNumber = '$codeword'
ORDER BY goie.CardNumber
But this gives multiple rows from the master table for each record in the associated tables. I thought about concatenate, but I need the data from the associated tables to be displayed individually. Not sure what to try next. Any help is much appreciated.
Sorry, and there is no way to do that like you want. JOIN's can't do that.
I suggest to keep solution with separate queries.
Btw - You could play with UNION operator,
http://en.wikipedia.org/wiki/Union_(SQL)#UNION_operator
P.s.
You could extract main data separately, then extract data from related tables at once using UNION. With UNIOM it will give one result row per each row in related table.
In order to join an two of the Detail tables together without generating duplicate rows, you will have to perform the following operation on each one:
Group on the foreign key to the Master table, and aggregate all other columns being projected onto the join.
Numeric columns are commonly aggregated with SUM(), COUNT(), MAX(), and MIN(). MAX() and MIN() are also applicable to character data. A PIVOT operation is also sometimes useful as an aggregation operator for this type of circumstance.
Once you have two of the Detail tables grouped and aggregated in this way, they will join without duplicates. Additional Detail tables can be added to the join by first grouping and aggregating them also, in the same fashion.
I have 5 different tables T_DONOR, T_RECIPIENT_1, T_RECIPIENT_2, T_RECIPIENT_3, and T_RECIPIENT_4. All 5 tables have the same CONTACT_ID.
This is the T_DONOR table:
T_RECIPIENT_1:
T_RECIPIENT_2:
This is what I want the final table to look like with more recipients and their information to the right.
T_RECIPIENT_3 and T_RECIPIENT_4 are the same as T_RECIPIENT_1 and T_RECIPIENT_2 except that they have different RECIPIENT ID and different names. I want to combine all 5 of these tables so on one line I can have the DONOR_CONTACT_ID which his information, and then all of the Recipient's information.
The problem is that when I try to run a query, it does not work because not all of the Donors have all of the recipient fields filled, so the query will run and give a blank table. Some instances I have a Donor with 4 Recipients and other times I have a Donor with only 1 Recipient so this causes a problem. I've tried running queries where I connect them with the DONOR_CONTACT_ID but this will only work if all of the RECIPIENT fields are filled. Any suggestions on what to do? Is there a way I could manipulate this in VBA? I only know some VBA, I'm not an expert.
First I think you want all rows from T_DONOR. And then you want to pull in information from the recipient tables when they include DONOR_CONTACT_ID matches. If that is correct, LEFT JOIN T_DONOR to the other tables.
Start with a simpler set of fields; you can add in the "name" fields after you get the joins set to correctly return the rest of the data you need.
SELECT
d.DONOR_CONTACT_ID,
r1.RECIPIENT_1,
r2.RECIPIENT_1
FROM
(T_DONOR AS d
LEFT JOIN T_RECIPIENT_1 AS r1
ON d.ORDER_NUMBER = r1.ORDER_NUMBER)
LEFT JOIN T_RECIPIENT_2 AS r2
ON d.ORDER_NUMBER = r2.ORDER_NUMBER;
Notice the parentheses in the FROM clause. The db engine requires them for any query which includes more than one join. If possible, set up your joins in Design View of the query designer. The query designer knows how to add parentheses to keep the db engine happy.
Here is a version without aliased table names in case it's easier to understand and set up in the query designer ...
SELECT
T_DONOR.DONOR_CONTACT_ID,
T_RECIPIENT_1.RECIPIENT_1,
T_RECIPIENT_2.RECIPIENT_1
FROM
(T_DONOR
LEFT JOIN T_RECIPIENT_1
ON T_DONOR.ORDER_NUMBER = T_RECIPIENT_1.ORDER_NUMBER)
LEFT JOIN T_RECIPIENT_2
ON T_DONOR.ORDER_NUMBER = T_RECIPIENT_2.ORDER_NUMBER;
SELECT T_DONOR.ORDER_NUMBER, T_DONOR.DONOR_CONTACT_ID, T_DONOR.FIRST_NAME, T_DONOR.LAST_NAME, T_RECIPIENT_1.RECIPIENT_1, T_RECIPIENT_1.FIRST_NAME, T_RECIPIENT_1.LASTNAME
FROM T_DONOR
JOIN T_RECIPIENT_1
ON T_DONOR.DONOR_CONTACT_ID = T_RECIPIENT_1.DONOR_CONTACT_ID
This shows you how to JOIN the first recipient table, you should be able to follow the same structure for the other three...
I am building a MS Access 2010 application with a MySQL backend using ADO. Thus far, I have been successfully binding and updating my ADO recordsets to forms. However, I have just created the first query that contains two tables with an INNER JOIN and I am unable to update the returned recordset when fields are returned from both tables. This first query I am able to successfully bind and update.
SELECT table_A.a, table_A.b
FROM table_A INNER JOIN table_B ON table_B.c = table_A.c;
However, the moment I add a field from table_B, the recordset can no longer be updated via the bound form. The new MySQL statement looks as follows.
SELECT table_A.a, table_A.b, table_B.a
FROM table_A INNER JOIN table_B ON table_B.c = table_A.c;
I have read the forums, and my query does not appear to be subject to the usual problems that would prevent the recordset from being updateable (i.e., a lack of primary keys, aggregate functions, SELECT DISTINCT, …). Some forums have suggested that my problem may be related to ambiguity between the recordsets, but I have not been able to confirm this and it seems like this should work. Any help is much appreciated.
Try this:
SELECT table_A.a, table_A.b, CONCAT(table_B.a, '') FROM table_A LEFT JOIN table_B ON table_B.c = table_A.c;
in the first query, the record set displayed / retrieved belongs to a single table, so it can be updated. however as in the second query, the recordset retrieved as a result of join in two tables (columns belong to 2 tables) therefore it can't be directly updated.
in order to update some data based on the data from another table, you can use the following query
Update Table_A,Table_B set Table_A.a=Table_B.a where Table_A.b=Table_B.b
I’m stumped on how to display a recordset via a mysql query that will show two different "real" usernames in a history table that has columns for multiple userIDs (ie createdbyUserID, and assignedtoUserID) – I can get one of them via a JOIN, but how do I JOIN etc to show both since they will likely be different username? Some other trick? Is it via sql or some other function/loop?
Currently:
SELECT nxt_act_dev_hist.Created, nxt_act_dev_hist.assignedtoUserID,
nxt_act_dev_hist.createdbyUserID, nxt_user.username
FROM nxt_act_dev_hist
JOIN nxt_user
ON nxt_act_dev_hist.createdbyUserID=nxt_user.UserID
I'm a newbie here if you can't tell.
You can join the same table twice, like this:
select
-- some other fields
createdByUser.UserName as CreatedByUserName,
assignedtoUser.UserName as AssignedToUserName
from
nxt_act_dev_hist
JOIN nxt_user as createdByUser
ON nxt_act_dev_hist.createdbyUserID = createdByUser.UserID
JOIN nxt_user as assignedtoUser
ON nxt_act_dev_hist.createdbyUserID = assignedtoUser.UserID
I'm not a mysql guy, but that should work.