Avatar billede Ulrich Seniormester
11. september 2016 - 21:18 Der er 12 kommentarer og
1 løsning

Trække data ud fra to tabeller i en database.

Jeg har en MS database .mdb hvor i der er to tabeller.
Jeg skal trække alle data ud af begge tabeller, men der skal sorteres efter (ORDER BY) et felt kun i den ene.
Jeg laver det hele i Classic ASP og VBScript.

NOGET MED: select * from A  select * from B ORDER BY cpr i tabel B

Hilsen Ulrich
Avatar billede terry Ekspert
12. september 2016 - 10:27 #1
Lets say you have a table (tblA) with two fields CPR and FLD2
and another table (tblB) with two fields FLD1 and FLD2

You can use a UNION query like this.

SELECT CPR, FLD2 FROM tblA UNION ALL
SELECT FLD1, FLD2 FROM tblB
ORDER  BY CPR
Avatar billede terry Ekspert
12. september 2016 - 10:30 #2
UNION ALL means that ALL records are in result if you didnt use ALL then duplicates would not be shown
Avatar billede Ulrich Seniormester
12. september 2016 - 22:03 #3
Hi and thanks for the answer.
It works but not ok.
The tables are not the same, and not same number of columns. The columns must also have similar data types and mine are not. Also, the columns in each SELECT statement must be in the same order, and its not.

I only want to get data from table1 but ORDER BYa colume in table 2.
Avatar billede terry Ekspert
13. september 2016 - 08:16 #4
"I only want to get data from table1 but ORDER BY a column in table 2"

In your original question you said you wanted ALL the date from both tables but now your saying you only want data from table 1

Can you give an example of your tables and data and also what you expect to see in the result.
Your also welcome to send an example to me ekspertenATsanthell.dk AT = @
Avatar billede Ulrich Seniormester
13. september 2016 - 09:07 #5
Yes sorry, my fault.

Table 1                                   
(Side_navn) (Måned) (År) (Side_Hits) (Dato_sidst_set) (Tid_sidst_set)

Table 2
(Side_navn) (Side_Hits) (Dato_sidst_set) (Tid_sidst_set)

Tables are the same except (Måned) (År) (Side_Hits) are different in both Tables..
Want all data from Table1 out listet, but ORDER BY (Side_Hits) in Table 2.
Avatar billede terry Ekspert
13. september 2016 - 11:36 #6
and now some data from both tables and what you want to see in result.

I dont really see how your going to be able to sort by [table 2].Side_hits if it isn't in the result though

The purpose of sorting is so you can view data in specific order but if data ([table 2].Side_hits ) isn't in result what is there to see!!!!
Avatar billede terry Ekspert
13. september 2016 - 11:40 #7
are tables related in any way ?
Avatar billede Ulrich Seniormester
13. september 2016 - 18:40 #8
You got right. Then of course i like to see data Table 2 (Side_hits) in my output and then ORDER BY that.
Avatar billede terry Ekspert
14. september 2016 - 10:14 #9
Not sure what you mean :-(

If the tables are related then which fields make up the relationship?

If there is no relationship then I dont think its possible
Avatar billede Ulrich Seniormester
14. september 2016 - 10:35 #10
Table 1                                   
(Side_navn)

Table 2
(Side_navn)

are related.
Avatar billede terry Ekspert
14. september 2016 - 12:19 #11
OK, and I assume that Side_navn in table 1 is unique?

You need to make a query. Add both tables to query then using mouse drag Side_navn from table 1 to Side_navn in table 2.

Then add the fields from both tables which you want to see in the query. You do that by clicking on each of the fields.
Now you need to choose Side_hits from table 2 as sort order.

Save the query and then if you right click in the query designer where tables are shown you can choose SQL view. This is the sql you can use in ASP but you can also select from the query
Avatar billede Ulrich Seniormester
14. september 2016 - 18:08 #12
Yes Side_navn in table 1 is unique.

I do not use designer but code direc.
Eg:
conn.execute("SELECT SUM(hit_count) as AllTotalSum FROM Table1")
Avatar billede terry Ekspert
14. september 2016 - 20:06 #13
if you dont want to use the designer which will give you the sql you ant then you should look at INNER JOINS

http://www.w3schools.com/sql/sql_join.asp
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester