If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Newbie question: duplicate values

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-30-11, 16:29
shamusom shamusom is offline
Registered User
 
Join Date: Mar 2011
Posts: 2
Newbie question: duplicate values

Using sqlite3, how do you search across multiple tables (>2) for a possible duplicate value that might exist in the same column (same column name exists in each table)

Is there a way to do this using sqlite3? How do you select all the tables and perform this query?
Reply With Quote
  #2 (permalink)  
Old 03-30-11, 16:34
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,407
I don't know if SQLite supports UNIONs but this is at lest standard ANSI SQL:

Code:
SELECT the_column, count(*)
FROM ( 
   SELECT the_column
   FROM table_1
   
   UNION ALL

   SELECT the_column
   FROM table_2

   UNION ALL
   
   SELECT the_column
   FROM table_3
) t
WHERE the_column = 'your_search_value'
GROUP BY the_column
HAVING count(*) > 1
Reply With Quote
  #3 (permalink)  
Old 03-30-11, 16:42
shamusom shamusom is offline
Registered User
 
Join Date: Mar 2011
Posts: 2
duplicate values

Thanks for that tidbit, I have something similar, but how do I select all the tables with a given name without having to type all of them in one by one? Know what I mean? All the tables start with a certain prefix, and I want to search all of those. Possible?

thx for the info.
Reply With Quote
  #4 (permalink)  
Old 04-01-11, 02:14
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
You could generate the SQL query by scanning the catalog, e.g.:
Code:
SELECT table_name
FROM    information_schema.tables
WHERE  table_name like '<your-prefix>%'
and then build the above query programmatically. With a bit of recursive SQL you can build the whole query in a single SQL statement, fetch that result and execute it dynamically as another query.

p.s: The ISO standard defines the catalog tables in a schema named INFORMATION_SCHEMA. Most systems use other schema names and may also have other table/column names.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
Reply

Tags
sqlite

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On