Geekzone: technology news, blogs, forums
Guest
Welcome Guest.
You haven't logged in yet. If you don't have an account you can register now.


davidcole

6037 posts

Uber Geek

Trusted

#125713 17-Jul-2013 08:08
Send private message

If I had a bunch of tables in a database that all have different columns names eg

Books
ID, Title, Author
1, Green Man, Frank
2, Contact, Carl Sagan

Cars
ID, Make, Model, Colour
3,Ford,Laser,Green
4,Holden, Commodore, Blue

Companies
ID, Name, Address, EmployeeCount
5,Green Acres, 10 House St, 20
6,McDonalds, Featherston St, 50

How could I return the ID of rows where any of the columns is Green (assuming that 'Green' is used as a colour of a car, and 'Green' is part of a name of a company and a book

So I'd expect to returns IDs, 1, 3 and 5.

Ideally a SQL lite based sql query - but I'm assuming something like this would be cross DB and I'm not sure if you'd use the FTS extentions for this or not.




Previously known as psycik

Home Assistant: Gigabyte AMD A8 Brix, Home Assistant with Aeotech ZWave Controller, Raspberry PI, Wemos D1 Mini, Zwave, Shelly Humidity and Temperature sensors
Media:Chromecast v2, ATV4 4k, ATV4, HDHomeRun Dual
Server
Host Plex Server 3x3TB, 4x4TB using MergerFS, Samsung 850 evo 512 GB SSD, Proxmox Server with 1xW10, 2xUbuntu 22.04 LTS, Backblaze Backups, usenetprime.com fastmail.com Sharesies Trakt.TV Sharesight 


Create new topic
robjg63
4100 posts

Uber Geek

Subscriber

  #857903 17-Jul-2013 09:48
Send private message

Whew - Its been a while since I looked at databases...
But my guess is that you would need to select the tabname and ID into a temp table for each of the 3 tables you are searching - and you would have to say select ID from tabname where col1 = 'green' or col2 = 'green'...etc
and repeat for each table.

Hmm - then I found this - I think its sqlserver though - its a procedure to do roughly what I suggested above:
http://vyaskn.tripod.com/search_all_columns_in_all_tables.htm




Nothing is impossible for the man who doesn't have to do it himself - A. H. Weiler




robjg63
4100 posts

Uber Geek

Subscriber

  #857905 17-Jul-2013 09:49
Send private message

This?
http://stackoverflow.com/questions/13514509/search-sqlite-database-all-tables-and-columns

Its using python to query the database though...




Nothing is impossible for the man who doesn't have to do it himself - A. H. Weiler


TwoSeven
1626 posts

Uber Geek

Subscriber

  #857918 17-Jul-2013 10:09
Send private message

Try something along the lines of the following (this is just a guess using T-SQL):

Select car.id, book.id, company.id FROM
(SELECT id FROM cars WHERE model like '%green%') as car,
(SELECT id FROM books WHERE colour like '%green%) as book,
(SELECT id FROM companies WHERE name like '%green%) as company

This will give you a result set but will contain duplicate values. In this example you will be able to determine which table the ID came from.


Personally I would probably just create a table function with three separate queries and output the result of a list of IDs.

One notes that there does not appear to be a logical relationship between any of the tables so one would question why one query is used.




Software Engineer
   (the practice of real science, engineering and management)
A.I.  (Automation rebranded)
Gender Neutral
   (a person who believes in equality and who does not believe in/use stereotypes. Examples such as gender, binary, nonbinary, male/female etc.)

 

 ...they/their/them...




davidcole

6037 posts

Uber Geek

Trusted

  #857922 17-Jul-2013 10:17
Send private message

TwoSeven: Try something along the lines of the following (this is just a guess using T-SQL):

Select car.id, book.id, company.id FROM
(SELECT id FROM cars WHERE model like '%green%') as car,
(SELECT id FROM books WHERE colour like '%green%) as book,
(SELECT id FROM companies WHERE name like '%green%) as company

This will give you a result set but will contain duplicate values. In this example you will be able to determine which table the ID came from.


Personally I would probably just create a table function with three separate queries and output the result of a list of IDs.

One notes that there does not appear to be a logical relationship between any of the tables so one would question why one query is used.


I guess the only problem with this solution is if you added a table or a column you'd have to update the code....




Previously known as psycik

Home Assistant: Gigabyte AMD A8 Brix, Home Assistant with Aeotech ZWave Controller, Raspberry PI, Wemos D1 Mini, Zwave, Shelly Humidity and Temperature sensors
Media:Chromecast v2, ATV4 4k, ATV4, HDHomeRun Dual
Server
Host Plex Server 3x3TB, 4x4TB using MergerFS, Samsung 850 evo 512 GB SSD, Proxmox Server with 1xW10, 2xUbuntu 22.04 LTS, Backblaze Backups, usenetprime.com fastmail.com Sharesies Trakt.TV Sharesight 


nzkc
1572 posts

Uber Geek


  #857953 17-Jul-2013 11:05
Send private message

This is a problem that SQL is bad at.  Yes you can do it, but performance will suffer.  What you're wanting is a full text query engine (e.g. Full Text Search in SQL Server, Lucerne, Solar etc).

That said, since you've said you're using SQLite I'm assuming its a small DB you can probably get away with sending lots of queries to each table.

itxtme
2102 posts

Uber Geek


  #858055 17-Jul-2013 14:00
Send private message

Why not use a UNION ?

It is a text search like stated above, you can union all tables that could ever have it in. But otherwise its normal practice to re-write querys when you change database structure

davidcole

6037 posts

Uber Geek

Trusted

  #858060 17-Jul-2013 14:09
Send private message

itxtme: Why not use a UNION ?

It is a text search like stated above, you can union all tables that could ever have it in. But otherwise its normal practice to re-write querys when you change database structure


You can dynamically union all the tables fairly easily enough, but how do you dyamically add all the where clauses for each field?

Also the example isnt great, but in this situation an new table can be added at any time to add new metadata about an object.

I'm tempted to probably do something like the python example that iterates the tables and columns.






Previously known as psycik

Home Assistant: Gigabyte AMD A8 Brix, Home Assistant with Aeotech ZWave Controller, Raspberry PI, Wemos D1 Mini, Zwave, Shelly Humidity and Temperature sensors
Media:Chromecast v2, ATV4 4k, ATV4, HDHomeRun Dual
Server
Host Plex Server 3x3TB, 4x4TB using MergerFS, Samsung 850 evo 512 GB SSD, Proxmox Server with 1xW10, 2xUbuntu 22.04 LTS, Backblaze Backups, usenetprime.com fastmail.com Sharesies Trakt.TV Sharesight 


 
 
 

Move to New Zealand's best fibre broadband service (affiliate link). Free setup code: R587125ERQ6VE. Note that to use Quic Broadband you must be comfortable with configuring your own router.
Regs
4066 posts

Uber Geek

Trusted
Snowflake

  #858194 17-Jul-2013 17:21
Send private message

What DBMS? SQL Server gives you queryable table and column definition system tables and a cursor could be used to cycle through all tables/columns in a database looking for a value.

I couldnt find the sql snippet i wrote a while back, so i googled one up for you:

http://stackoverflow.com/questions/709120/how-to-search-for-one-value-in-any-column-of-any-table-inside-one-ms-sql-databas




TwoSeven
1626 posts

Uber Geek

Subscriber

  #858234 17-Jul-2013 18:21
Send private message

davidcole:
TwoSeven: Try something along the lines of the following (this is just a guess using T-SQL):

Select car.id, book.id, company.id FROM
(SELECT id FROM cars WHERE model like '%green%') as car,
(SELECT id FROM books WHERE colour like '%green%) as book,
(SELECT id FROM companies WHERE name like '%green%) as company

This will give you a result set but will contain duplicate values. In this example you will be able to determine which table the ID came from.


Personally I would probably just create a table function with three separate queries and output the result of a list of IDs.

One notes that there does not appear to be a logical relationship between any of the tables so one would question why one query is used.


I guess the only problem with this solution is if you added a table or a column you'd have to update the code....


In that case I wouldn't use a relational database, I'd probably use an OODB or (if a SQL is required) an ORM layer.

An example of the latter is using .Net LINQ and reflection,  one can simply open a connection to the persistence layer (in this case database), bring back an entity set (collection of tables) and then make the query for each entity returned.  In Microsoft terms this is called Generics.

edit: As mentioned in another post +1 for union I suspect.




Software Engineer
   (the practice of real science, engineering and management)
A.I.  (Automation rebranded)
Gender Neutral
   (a person who believes in equality and who does not believe in/use stereotypes. Examples such as gender, binary, nonbinary, male/female etc.)

 

 ...they/their/them...


Create new topic





News and reviews »

Air New Zealand Starts AI adoption with OpenAI
Posted 24-Jul-2025 16:00


eero Pro 7 Review
Posted 23-Jul-2025 12:07


BeeStation Plus Review
Posted 21-Jul-2025 14:21


eero Unveils New Wi-Fi 7 Products in New Zealand
Posted 21-Jul-2025 00:01


WiZ Introduces HDMI Sync Box and other Light Devices
Posted 20-Jul-2025 17:32


RedShield Enhances DDoS and Bot Attack Protection
Posted 20-Jul-2025 17:26


Seagate Ships 30TB Drives
Posted 17-Jul-2025 11:24


Oclean AirPump A10 Water Flosser Review
Posted 13-Jul-2025 11:05


Samsung Galaxy Z Fold7: Raising the Bar for Smartphones
Posted 10-Jul-2025 02:01


Samsung Galaxy Z Flip7 Brings New Edge-To-Edge FlexWindow
Posted 10-Jul-2025 02:01


Epson Launches New AM-C550Z WorkForce Enterprise printer
Posted 9-Jul-2025 18:22


Samsung Releases Smart Monitor M9
Posted 9-Jul-2025 17:46


Nearly Half of Older Kiwis Still Write their Passwords on Paper
Posted 9-Jul-2025 08:42


D-Link 4G+ Cat6 Wi-Fi 6 DWR-933M Mobile Hotspot Review
Posted 1-Jul-2025 11:34


Oppo A5 Series Launches With New Levels of Durability
Posted 30-Jun-2025 10:15









Geekzone Live »

Try automatic live updates from Geekzone directly in your browser, without refreshing the page, with Geekzone Live now.



Are you subscribed to our RSS feed? You can download the latest headlines and summaries from our stories directly to your computer or smartphone by using a feed reader.