Sqlite: select same colume from many tabls - sqlite

Let's say we have tables: t1(_id, url), t2(_id ,url), t3(_id, url), ...
I wanna get all url from tables:
select url from t1 union select url from t2 union select url from t3 union ...
It's fine, but too long if I have 3 more tables. So how can I get all url in all table?

If you need data from all tables, you must name all tables somewhere in the FROM clause. SQLite has no way of knowing wich tables you want to SELECT from if you don't name them.

Related

BigQuery - reshape table to vector

I have a table with
zip_client zip_supplier zip_store
12345 56432 42374
35424 null 12345
etc
And I need to get overall unique zips.
Is there a simple way to do it within a query?
Currently I download the result into Sheets and do it there.
If there was some kind of reshape in BigQuery it would be very easy, but I could not find how to do it.
Thanks
Below is for BigQuery Legacy SQL
#legacySQL
SELECT
type, zip
FROM
(SELECT 'client' AS type, zip_client AS zip FROM yourTable),
(SELECT 'supplier' AS type, zip_supplier AS zip FROM yourTable),
(SELECT 'store' AS type, zip_store AS zip FROM yourTable)
WHERE NOT zip IS NULL
if you will have chance or decide to migrate to BigQuery Standard SQL - see below
#standardSQL
SELECT zip.type, zip.zip
FROM yourTable,
UNNEST([STRUCT<type STRING, zip INT64>
('client', zip_client),
('supplier', zip_supplier),
('store', zip_store)]) AS zip
WHERE NOT zip.zip IS NULL
Using standard SQL:
SELECT DISTINCT zip
FROM YourTable
CROSS JOIN UNNEST([zip_client, zip_supplier, zip_store]) AS zip;
You can put any number of columns inside the UNNEST depending on the structure of your table.

SQL Server Stored Procedure, selecting rows from multiple tables

I have a stored procedure that is pulling data from one table, now I have got the same data collected in a different table, as the IDs are different I cannot put all data from the second table into the first one.
So now I have to have two select statements in one stored procedure.
Although the corresponding data is same but the column names in both table are different.
For instance BRIEF_TITLE would be briefTitle in the second table.
How can I merge the data from two different tables into one?
The result is bonded to ASP.net grid view control.
As the comments above state, you need something like this:
select BRIEF_TITLE from t1
union all
select briefTitle from t2
This will give you a column name of BRIEF_TITLE, but if you want something else, then add an alias to the first select
select BRIEF_TITLE as ShortTitle from t1
union all
select briefTitle from t2

How to find which table that contain specific data

Lets say i have Database with to tables (division_1,division_2)
each table have the same columns ( id, name , salary)
and Jone is person in one of these tables
is there a query in SQL that return in which division Jone is?
Use a union to select from both tables and see which contains the record
select 'division1' as div from division_1 where name = 'Jone'
union
select 'division2' as div from division_2 where name = 'Jone'
BTW if you have the possibility to change the DB design, then do it. There should only be one division table with a column that indicates which one it is.

How to select distinct records from two tables, combine into one column, and eliminate all records that exist in another table using SQLite?

Here is my existing query which successfully selects distinct records from two tables and combines them into one column:
SELECT index_text AS unique_text FROM words
UNION
SELECT c1index_text FROM words_content
ORDER BY unique_text
Now I want to eliminate all records WHERE body NOT IN (SELECT body FROM sms) (or NOT EXISTS, whatever works is fine). The problem is that no matter what I try, either I get a syntax error whenever I attempt to use parentheses, or it will not recognize sms.body (even if I precede every column by its parent table). I'm thinking some SQLite limitations may be making this harder than it needs to be, but there has to be a workaround. Below are queries I have tried unsuccessfully (I have also tried numerous variations of these queries to no avail):
SELECT index_text AS unique_text FROM words
UNION
SELECT c1index_text FROM words_content
WHERE body NOT IN (SELECT body FROM sms)
ORDER BY unique_text
Results in error: No such column: body
SELECT words.index_text AS unique_text FROM words
UNION
SELECT words_content.c1index_text FROM words_content
LEFT JOIN sms
ON sms.body=unique_text
ORDER BY unique_text
Results in error: No such column: unique_text
How do I join to an alias column and show only records that do not exist in sms.body? Thanks,
If you use where or join clauses in UNIONs you have to apply them to both select statements.
SELECT index_text AS unique_text
FROM words
where index_text NOT IN (SELECT body FROM sms)
UNION
SELECT c1index_text
FROM words_content
WHERE c1index_text NOT IN (SELECT body FROM sms)
ORDER BY unique_text

SQL Server Creating Indexes Catalogs with joined tables

So at the moment I am building a Advanced Search in .NET, and getting the results is just proving a bit slow so was looking at creating indexes on the tables.
I.e went to tables and define full text index.
So now I have my catalog with the 5 tables and selected columns.
But I cant see how this catalog actually joins these tables ?
I.e. in my "slow" stored procedure I could have
select *
from table1
inner join table2 ON table1.id = table2.linkedID
etc for other tables ?
and now I guess I can go
select * from catalogName
but how does catalogName know what columns to join for the inner join etc
You don't query the fultext catalog directly, you use the fulltext functions in your query, like CONTAINS, CONTAINSTABLE, FREETEXT and FREETEXTTABLE:
SELECT field, field, field
FROM table
WHERE CONTAINS(field, 'some text');
Full text has nothing to do with joining tables and if your query is slow because you join 5 tables then FT is unlikely to help at all.

Resources