I like to create an empty table from a (pivot) query in Microsoft Access (using Office 365 1902). With the help of this post I found a solution with one shortcoming:
SELECT TOP 1 * INTO tblNew FROM qryMyQuery;
The shortcoming is that tblNew contains one record which needs to be deleted to get an empty table. Access 365 can not process SELECT TOP 0 * INTO.
I post this primarily because other people may have the same question and run into the same problem. I can work with my solution, but maybe somebody has a better one?
The solution is very simple:
SELECT * INTO tblNew FROM qryMyQuery WHERE False;
Since the WHERE condition matches 0 rows, 0 rows get copied.
Related
I have a table in MySQL with a field "Ordering" These are just auto incremented numbers. Now I wonder if there is a query to change the values from the last to the first...
So the entry with ordering 205 should become 1, 204 -> 2 and so on...
It's actually not an auto-increment. The problem is I started adding projects from the current website. From page 1 to page 20, but the first item on page 1 is the latest. The way I added the new projects, the newest is on the last page..
If the ordering field is switched, the new items added will be correctly numbered again and added to the front page. It's just a wrong way I started adding old projects...
Structure
Examples of the content
I can't comment due to limitations, but i really agree with #Abhik Chakraborty.
You don't want to do this. Just use the order by as he suggested.
Example:
SELECT * FROM tableName
ORDER BY columnName DESC;
Just in case you would like to know more about it: http://www.w3schools.com/sql/sql_orderby.asp
Try this as one statement call:
SET #MaxSort := (SELECT MAX(Ordering) FROM MyTable);
UPDATE MyTable t set t.Ordering = (#MaxSort + 1 - t.Ordering);
This will work if field doesn't have unique constraint.
But this field, should not be an auto_increment field at first place. Auto increment is increasing NOT decreasing counter. Except if you just try to fix existing data and the new records will be increasing.
Additional explanation
Thanks for pointing it out. Multiple query inside single query statement doesn't work with php_mysqli and it is not used because of potential MySQL injection attack if servers allows it. Maybe you can setup PHPMyAdmin to use PHP PDO.
I can use multiple queries, but I'm using PHP PDO or DBeaver database manager.
I can only suggest to supply MaxSort manually (since this is one time job anyway):
UPDATE
MyTable t
set
t.Ordering = 254 - t.Ordering + 1;
I'm working on an MS Access 2010 database, and I have 5 tables I need to query.
Table 1 has an ID, name, a several other unrelated columns.
Tables 2 through 5 have their own IDs, the ID of the related record from Table 1, a date field, and then other miscellaneous fields.
I am trying to build a query to return the ID and name from table one, and the dates from tables 2-5.
I didn't really think that there would be any issues, but here I am. I have not done anything special with the query, just added the tables/columns to the Query Design view.
The query is working, sort of, however if the user has not created a related record with a matching ID in any of the related tables that record in table a is ignored.
I would like the query to return all records from table 1, and show me the dates from tables 2-5, even if one or all of them do not have a related entry.
I hope this makes sense... Any assistance is greatly appreciated! Thanks! :)
If I correctly understand what you are asking for. Open the query in design view, double click on the line connecting table 1 to another table and select. All from table 1 and only those records...
I think that should give you what you are looking for. Otherwise you will probably need to post some more data about your other tables.
I have some data in mysql and was wondering whether I can create a new table and then add some type of formula that can count existing tables? For example, I have a table called 'Reviews' which contains 150 rows. Can I have a separate table within mysql that can tell me there are 150 rows without me logging in to see this? Any help appreciated.
You can create a VIEW that contains the count and has other permissions than the table;
CREATE VIEW ReviewCount AS SELECT COUNT(*) FROM Reviews;
This view will have the read access of the defining user, and you can hide the underlying table from the other user, while he can still use the view to get the count.
The thing is, as far as the database is concerned, the other user still has to be logged in. If you want truly anonymous access, you'll have to wrap the database inside your own code, for example a web service.
EDIT: Your comment indicates that what you're looking for is simply a summary table of counts from a number of tables. In that case, you can simply create the view as;
CREATE VIEW test_count AS
SELECT (SELECT COUNT(*) FROM reviews) reviewCount,
(SELECT COUNT(*) FROM records) recordCount,
...
;
An SQLfiddle to test with.
I would like to store random numbers in one MySql table, randomly retrieve one and insert it into another table column each time a new record is created. I want to delete the retrieved number from the random number table as it is used.
The random numbers are 3 digit, there are 900 of them.
I have read several posts here that describe the problems using unique random numbers and triggering their insertion. I want to use this method as it seems to be reliable while generating few problems.
Can anyone here give me an example of a sql query that will accomplish the above? (If sql query is not the recommended way to do this please feel free to recommend a better method.)
Thank you for any help you can give.
I put together the two suggestions here and tried this trigger and query:
CREATE TRIGGER rand_num before
INSERT ON uau3h_users FOR EACH ROW
insert into uau3h_users (member_number)
select random_number from uau3h_rand900
where random_number not in (select member_number from uau3h_users)
order by random_number
limit 1
But it seems that there is already a trigger attached to that table so the new one cause a conflict, things stopped working until I removed it. Any ideas about how accomplish the same using another method?
You are only dealing with 900 records, so performance is not a major issue.
If you are doing a single insert into a table, you can do something like the following:
insert into t(rand)
select rand
from rand900
where rand not in (select rand from t)
order by rand()
limit 1
In other words, you don't have to continually delete from one table and move to the other. You can just choose to insert values that don't already exist. If performance is a concern, then indexes will help in this case.
More than likely you need to take a look into Triggers. You can do some stuff for instance after inserting a record in a table. Refer this link to more details.
http://dev.mysql.com/doc/refman/5.0/en/create-trigger.html
Using Access 2003
I want to get a table value from the two databases
Database – 1
Emp_Table
Database – 2
Customer_Table
Select * from Database-1.Emp_Table, Database-2.Customer_Table
The above query is showing error in the Access. I tried a Join query also, it showing error.
Can any one to solve this problem?
Need Query Help.
Try using square brackets -
SELECT * FROM [Database-1].[Emp_Table], [Database-2].[Customer_Table]
Or, try this.
First, assuming the query is running in Database 1, you will need to create a "linked table" to link to Database 2's table in Database 1.
Once you do that, you can write it simply as:
Select * from Emp_Table, Customer_Table
Since you are "in" database 1, you won't have to qualify Emp_Table, and since you have Database 2's Customer_Table linked in, you won't have to qualify it either.
I don't have Access 2003, but in Access 2007 you can do this:
Click on the "External Data" tab.
Click on the "Access" icon.
Choose the location of your second Access database.
Select "Link to a data source by creating a linked table".
This should add the tables in your second database linked in your original one. You can then write queries to query data from either one or both like you normally would. I'm sure the same functionality is available in Access 2003, just a slighty different visual route to achieve the same thing.
Is there some relationship between the tables, or do you want just a dump of the whole table? Also, post the join you tried and the error you got, it would help in the troubleshooting...
If you want all records from both tables, you would need to use a UNION query like this:
Select * from Database-1.Emp_Table;
UNION Select * from Database-2.Customer_Table;
This assumes that there are the same number of columns in both tables. If not change the * to the specific columns you want to list from each table.