Selectively searching from a set of tables in MS Access - ms-access

I wanted to know if it's possible to have one query which I could design so that it would take a certain record from only one table i.e. I have 3 tables and my query, based on a certain parameter would choose one table to take the records from.

You can do this if all tables have equal field names using a UNION query, and by adding a field that specifies from which table the row originated:
PARAMETERS ParamTableName VARCHAR(255);
SELECT * FROM (
SELECT *, "Table1" As TableName FROM Table1
UNION ALL
SELECT *, "Table2" As TableName FROM Table2
UNION ALL
SELECT *, "Table3" As TableName FROM Table3
) TheTables
WHERE TableName = ParamTableName

Related

Select the same columns from a list of tables

I am looking to run this query on a list of tables.
SELECT Description,Code,count(*) as count
FROM table1
group by Description,code
having count(*) > 1
I will have to run this query on 30+ different tables, I was wondering If I could change the from statement and just list off the table names.
In addition, is there some functionality that will add the name of the table that it came from in a seperate column to distinguish where the results came from?
Thanks in advance
You might use UNION ALL to put it together. Unless you need some dynamic table selection.
SELECT Description,Code,count(*) as count, 'table1' as tableNane
FROM table1
group by Description,code
having count(*) > 1
UNION ALL
SELECT Description,Code,count(*) as count, 'table2' as tableNane
FROM table2
group by Description,code
having count(*) > 1
...
Actualy I like #Shubhradeep Majumdar version. It will generate more concise code.
SELECT Description,Code, Count(Code), tableName FROM (
SELECT Description,Code, 'table1' as tableName
FROM table1
UNION ALL
SELECT Description,Code, 'table2' as tableName
FROM table2
) tables
GROUP BY tableName, Description, Code
HAVING COUNT(Code) > 1
But there might be a little catch to it. It is more elegant code, but it might actually be slower than first version. The problem is that tableName is appended at every record before grouping while in my first version you do that on already processed data.
Carrying over from #Marek's answer, You could first append all the tables to a table with union all.
select *, 'tab1' as tabnm from tab1
union all
select *, 'tab2' as tabnm from tab2
union all
select *, 'tab3' as tabnm from tab3
-- and so on...
And then use your code to process that final table.
will save you a great deal of time.
EDITED with a column specifying the table name

JOIN / UNION SELECT all rows from two tables with different number of columns

I am trying to join or union the results of two differently structured tables, order them by a their common pubdate column, but get an error message:
The used SELECT statements have a different number of columns
SELECT * FROM news WHERE published='1' AND image!='' AND featured='1'
JOIN
SELECT * FROM videos WHERE published='1'
ORDER BY pubdate DESC
How can I edit my query so that I can run my fetch array and retrieve the corresponding rows afterwards (the rows to be fetched is then decided on another common row that both tables share)?
You can UNION ALL tables with a different number of columns by adding the missing columns in the select list. I mean, suppose you have:
SQL> create table one ( c1 integer , c2 integer, c3 integer ) ;
SQL> create table two ( c1 integer , c2 integer ) ;
The following UNION ALL won't work:
SQL> select * from one union all select * from two ;
[mysqld-5.5.27]The used SELECT statements have a different number of columns
But this one is ok:
SQL> select * from one union all select *, null from two ;
Note we did add NULL for the missing (c3) column on table two. You can add whatever you want...
Since you not provided sample data, we can't suggest you correct way what and how to use, but I noticed that:
With JOIN you have incorrect syntax. Should be like:
SELECT *
FROM tbl1 t1
INNER JOIN tbl2 t2 ON t1.Id = t2.Id
If you want to use UNION - order of columns and number of columns should match in both tables, so you have to provide column names in both tables, in following:
SELECT Col1, Col2, Coln
FROM tbl1
UNION
SELECT Col1, Col2, Coln
FROM tbl2

SQL Join 2 tables with almost same field

I need to join two tables in SQL. There are no common fields. But the one table have a field with the value krin1001 and I need it to be joined with the row in the other table where the value is 1001.
The idea behind the joining is i have multiple customers, but in the one table there customer id is 'krin1001' 'krin1002' and so on, in this table is how much they have sold. In the other table there customer is is '1001' '1002' and so on, and in this table is there name and adress and so on. So it will always be the first 4 charakters i need to strip from the field before matching and joining. It might not always be 'krin' i need it to work with 'khjo1001' also, and it still needs to join on the '1001' value from the other table.
Is that possible?
Hope you can help me.
You need to use substring:
ON SUBSTRING(TableA.Field, 5, 4) = TableB.Field
Or Right:
ON RIGHT(TableA.Field, 4) = TableB.Field
You can also try to use CHARINDEX function for join operation. If value from table1 contains value from table2 row will be included in result set.
;WITH table1 AS(
SELECT 'krin1001' AS val
UNION ALL
SELECT 'xxx'
UNION ALL
SELECT 'xyz123'
),
table2 AS(
SELECT '1001' AS val
UNION ALL
SELECT '12345'
UNION ALL
SELECT '123'
)
SELECT * FROM table1 AS t
JOIN table2 AS T2 ON CHARINDEX(T2.val, T.val) > 0
Use it as:
SELECT
*
FROM table t1
INNER JOIN table t2 ON RIGHT(t1.col1, 4) = t2.col1;

add column to say which table a union result is from

How can I select which table each result is from, when I use a UNION command to search multiple tables?
For example, if there are results from both tables, how can I add a column that will say (or differentiate between) whether it is from tableA or tableB.
try this one, simply add a virtual column for the name of the table.
SELECT *
FROM
(
SELECT *, 'tableA' as tableName FROM tableA
UNION ALL
SELECT *, 'tableB' as tableName FROM tableB
UNION ALL
SELECT *, 'tableC' as tableName FROM tableC
) s
WHERE colName = 'hello'

Create table as ( using a column and table name)

I need to build a table from several tables in MySql and I want it has two columns like:
-------------------
name | table_name |
-------------------
I'm doing this:
Create table teams as
(
Select name from table1
union
Select name from table2
union
Select name from table3);
How could I include each table name as a second column??
Thank you!
Include a quoted string literal. Be sure to also give it a column alias, which will be used as the column name in the resultant table.
CREATE TABLE teams AS
/* Quoted string literal with column alias */
(SELECT name, 'table1' AS `tablename` FROM table1)
UNION
(SELECT name, 'table2' AS `tablename` FROM table2)
UNION
(SELECT name, 'table3' AS `tablename` FROM table3);
Note that since you are in effect adding a second value to each row which differentiates it from potentially similar rows in the other tables, the UNION is now the eqivalent of a UNION ALL, and duplicate rows won't be de-duped as the plan UNION would have. Just beware, that the results may differ from what your original UNION produced.