I have tried to find this answer, but I can't seem to find what I am needing.
I am trying to select a row from a MySQL database, then update a counter in that row. All of this needs to be done in a view so I can call it like a normal select.
I am working with an RSS feed where I have total access to MySQL,but zero access to the PHP that outputs the call (I didn't built it, but I inherited the problem :/). To make the call work, I pass the table name into the RSS feed, and it pretty much does this:
SELECT * (table name I pass in).
Because I can't access the PHP, I can't add an update statement to mark the record I selected as used. All of this is so I don't display the same record twice until all the records have been selected.
I am hoping to be able to do this all in a view. I would like to be able to select a row that hasn't been selected, then update that with a counter so I don't select it again.
Example:
SELECT title,counter FROM example WHERE counter = 0 UPDATE example SET counter++
Any help would be great!
EDIT: I need to be able to update the same row that I select.
Related
I have a two table scenario with a typical parent / child relational setup:
tblGroup - idGroup (autonumber, PK); GroupName (Short Text), Rank (Number)
tblRange - idRange (autonumber, PK), idGroup (FK -> tblGroup.idGroup), RangeName (Short Text), Rank (Number)
What I am doing on the parent (tblGroup) table is using a data macro to add the rank using the BeforeChange event:
IF
IsInsert
SetField - Rank
- DMAX(Rank, [tblGroup])+1
This works nicely and I can happily use a parametized INSERT query to add rows to the table and not have to worry about duplicate ranks and so forth.
What I would like to be able to do but cannot figure out how, is to have a data macro do the same thing for the child (tblRange) table, with the rank being set to the new highest for the parent group the child record belongs to.
If I use the same DMAX approach as I have above I am supposed to be able to set a criteria as a third option, acting like a where clause, to limit the lookup / calculation. How can I refer to the specific idGroup I am working with in tblRange in the macro? I cannot seem to figure out how to reference the new records value for this in the macro.
Something like DMAX(Rank, [tblRange], ???How_to_refer_to_idGroup_Properly???)+1
Any help greatly appreciated
Cheers
The Frog
I figured out a way to do this. Thankyou caffeinated beverages!
The reason for the strange error messages is due to limitations in the Data Macro processing, specifically in the BeforeChange event. The solution is as follows:
Create a query that selects MAX rank (MaxRank) and GROUP BY for the idGroup (ParentID)
The resultant query produces two columns of data: [MaxRank] and [ParentID]
There will be a row for every idGroup with the maximum Rank for each
Create a BeforeChange data macro
Set the following:
IF IsInsert
LookupRecord
Lookup Record In - qryGetMaxRank (or whatever you called your query)
WHERE - [qryGetMaxRank].[ParentID] = [tblRange].[idGroup]
Set Field
Name - [tblRange].[Rank]
Value - [MaxRank] + 1
The BeforeChange event cannot handle parameters for a query, and I am guessing that this applies in some form the to DMAX function here too. The use of a query that does not use any parameters, and then using the LookupRecord WHERE clause to do the filtering provided the single row result needed. The [MaxRank] value from the returned result is then able to be used to set a new value for the field.
Bit of a workaround but it does allow someone to work with the data either through a form or through the datasheet view and not create a problem.
**In answer to if this is a multi-user DB - it is not. It is just me working with it. If / when the solution is scaled up to something requiring multi-user I will likely recreate the BE in SQL Server or MySQL and use stored procedures for all data I/O. Happy to keep Access as the FE and compile into an application (using the runtime for clients), but I am a fair way off from having to do that yet. Very early stages of development at this time.
Cheers to everyone for the pointers. They helped me figure this out. Hopefully this will be of use to someone else in the future.
PS: If you need to use a parametrized query in a data macro it looks like the best bet is with the AfterInsert event or AfterUpdate event as they can support parameters.
I am looking for an Idea to handle data in DB directly.
Here is my use case
I have table “EVENT_REGISTERED” with column (ID, Event_name,Event_DateTime)
I want to display the EVENT_REGISTERED in the fronted end whose date and time is not passed. i.e. I want to display only Upcoming event not Historical events.
Of course this can be handle with JS code before displaying.
But What I want is there should be some sort of a trigger which will delete the Instance form the “EVENT_ REGISTERED” table and copy it to another Table “HISTORICAL_EVENT”
I cannot Create an MY SQL EVENT to do this as it like batch job and I cannot run this every 5 mins as there can be more than 10000 rows in there.
I see Trigger option as well, I am not sure how to use this as it says that it will be activated after the specific action is executed. Here the specific action is CURRENT_DATETIME == EVENT_DATETIME.
Can anybody give me a direction or any sort of alternative way to achieve this?
**I am not an Expert of MySQL*
Thank you
Regards
Prat
Don't start moving rows between tables. Simply run a query:
select er.*
from event_registered
where er.event_datetime > now();
With an index on (event_datetime), performance should be fine.
I posted a couple times over the last couple of days about some trouble I'm having with an inventory database that I'm trying to make at work. I have very little experience, so have been mostly following YouTube tutorials and walk throughs online.
Essentially I have a table that will be filled with inventory information such as ID number, manufacturer, model number, etc. I then have a form that has a text box for each field in the table that the user will fill in and then an Add, Delete and Edit button that will allow them to either Add a new record by filling in the text boxes and clicking Add or select a current record in the table from a combo box or list and edit it or delete it by clicking the associated buttons.
I tried to code this first and kept getting an error for the past two days no matter how much I tried to fix it and what advice I got on here, so now I'm at the point where I'm just using queries to do it.
Right now I have a main table, temp table, the form, and then these queries:
1. delete query that will clear the temp table
2. append query that will take the records from my temp table and add them to my main table
3. append query that will take the selected record on form and add it to the temp table
4. update query that will update the fields on the main table to the data in the temp one
5. delete query that will delete the record you select on the main form from the main table
This was my plan:
for adding a new record
1. run query no.1
2. load the form
3. if OK button clicked:
4. run query no.2
for editing
1. run query no.1
2. run query no.3
2. load the form
3. if OK button clicked:
4. run query no.4
for deleting:
1. load a msgbox with the YesNo options
2. if Yes button clicked:
3. run query no.5
The one I'm having trouble with is query number 3. I've made the query correctly to the best of my knowledge, but when I fill out the form and click add it tells me the query is going to append 0 rows and then it doesn't add anything to the temp table.
I also can't seem to figure out how to take the data from the temp table and move it to the main table using the query.
I'm linking to an IMGUR post with screenshots in it here:
http://imgur.com/a/ffWge
Any ideas?
I hesitate to answer this since there are too many points to cover. Gustav already gave great advise about learning to use Access Forms like they are intended. But from the screenshots, it looks like you misunderstand something fundamental about the Access query designer. Even though you shouldn't have to write any queries for want you want to do, at some point you will need to understand how to properly design a query.
If you want the query to insert FROM the form INTO the table, then the table field references should not be in the criteria, they should be like this:
Field: [Forms]![frmInventory]![ICN]
Table: {leave blank}
Append To: ICN
Criteria: {leave blank}
Also, remove [tblInventoryTemp] from the query window since you already specified the destination table in the Append Query pop-up window. The SQL should look something like this:
INSERT INTO tblInventoryTemp ( ICN, model, serial, ... )
SELECT [Forms]![tblInventory]![ICN], [Forms]![tblInventory]![model], [Forms]![tblInventory]![serial], ...
Likewise for the other query to copy from temp to the main table. You should have already specified the destination table in the pop-up window, so remove [tblInventory] from the query window. Also remove the list of fields you have in the Criteria row. The SQL should look something like this:
INSERT INTO tblInventory ( manu, model, serial, ... )
SELECT tblInventoryTemp.manu, tblInventoryTemp.model, tblInventoryTemp.serial, ...
FROM tblInventoryTemp
Access will likely add more details, but the general form of the SQL should match what I have.
I have a project with various parts.... One of which is the calculate the area of all the polygons on a map. When I run the query "select st_area(nycpp.the_geom) from nycpp;" I get a list of all the areas.
Next, I tried adding the results of the query to the nycpp table with
UPDATE nycpp SET area_sizes = (select st_area(nycpp.the_geom) from nycpp);
but get the error -- "more than one row returned by a subquery used as an expression"
I figured out why am I getting the error... what I can not figure out is how write a script that will update all 12K+ records....
Can someone give an example or a link to info on updating multiple records
The database I'm using is PostGIS
Thanks
Chris
You are making it to complicated. Try:
UPDATE nycpp set area_sizes=ST_Area(the_geom);
This may be a stupid question but is it possible to store a count query in a field in a table such that when the result of the count changes the result will update itself?
Thread(ThreadID,NumMessages)
Message(MessageID,ThreadID,MessageText,PreviousMessage)
I want to update the Thread.NumMessages field any time a message with the corresponding ThreadID gets added or removed. I know I can do this by incrementing/decrementing the Thread.NumMessages field of by using a count query
SELECT COUNT(*), FROM SCHEMA.Message WHERE ThreadID='SOMETHREADID'
But is there anyway of setting up the NumMessages field so this is kept up to date without it being done explicitly at every addition and delete?
Thanks
Graeme
yes, you can use a view as the implementation of your Thread table.
http://dev.mysql.com/doc/refman/5.1/en/create-view.html
Create view thread_view
select
count(*) as NumMessages,
threadID
from message
group by threadID
Triggers!
You're wanting a trigger. This should help you with your searching (knowing what to look for is half the battle). http://www.databasedesign-resource.com/mysql-triggers.html
Basically, a trigger fires any time a designated event (such as insert) on a table is tripped, and executes a stored procedure.
your stored procedure would then check to make sure it's something it cares about (you can add all sorts of logic here), then does something (increment that value?) before MySQL executes the original query that triggered it in the first place.
http://www.roseindia.net/mysql/mysql5/triggers.shtml