Sponsored Content
Top Forums Programming Query to SELECT only Column Names that Contain a Specific String? Post 302879240 by mrm5102 on Wednesday 11th of December 2013 11:30:44 AM
Old 12-11-2013
Hey Robin, thanks for the reply!

Sorry for the delay in my reply, the weather has been pretty crazy here the last few days...

Thanks, I used your keyword's search and found a few things to try, but still having a little trouble. I modified the Google search
a bit and found an example of someone trying to list ALL tables where they have column names with a certain string. Which is
pretty close to what I'm trying, but I'm just looking to search within one Table and not the whole DB.

This is the example I found. It lists all Tables and their column names that are LIKE "%string%". So how can I modify this to
search only within one Table instead of the whole DB. I only know the very basics of DB Querying so I'm not really sure...
Code:
SELECT c.name AS ColName, t.name AS TableName 
FROM sys.columns c 
    JOIN sys.tables t ON c.object_id = t.object_id
WHERE c.name like '%email%'

So that gives me a list of Column Names containing the string "email" and their corresponding TableName they are in... Any
idea how I can modify it to display just one Table and list column names and their data where the colname contains the string "email"..?

Any suggestions would be much appreciated!

EDIT:
After playing a bit with the given SQL Query above, I was able to add the part below in blue to include ONLY the table I want, but I'm still
trying to figure out how to include the data for those columns as well...
Quote:
SELECT c.name AS ColName, t.name AS TableName
FROM sys.columns c
JOIN sys.tables t ON c.object_id = t.object_id
WHERE c.name like '%email%' AND t.name = 'Table_Name_I_Want'
Thanks Again,
Matt

Last edited by mrm5102; 12-11-2013 at 12:44 PM..
 

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

How to select only those file names whose name contains only numbers

Hi Guru's, Before writing to this forum I have searched extensively on this forum about my problem. I have to write a shell script which takes out only those file names from the given directory which contains only numbers. For example, In the given directory these files are present: ... (4 Replies)
Discussion started by: spranm
4 Replies

2. UNIX for Dummies Questions & Answers

replacing string in a column on a specific line

hi, i currently have a file with columns similar to this customer name owed CID123 John 300 CID342 harry 500 at present i use use awk to find the amount owed by the customer using the customer ID (CID). if the customer spends more money how would i go about using sed/awk etc to... (2 Replies)
Discussion started by: skinnygav
2 Replies

3. UNIX for Dummies Questions & Answers

How to remove a string from a specific column in a file

Hello, A basic query. How can I remove a string from a specific column. For example, remove "abcd" just from column 2 in example file: abcd abcd1 abcd abcd2 abcd abcd3 to get output: abcd 1 abcd 2 abcd 3 Thank you!:) (4 Replies)
Discussion started by: auburn
4 Replies

4. Shell Programming and Scripting

Select a specific part of the string and print it

Hi all, I have a string that looks like: #!/bin/sh options="arguments: --user=alpha --group=beta --prefix=/usr/share --proxy-path=/proxy --proxy-tmp=/tmp --conf-path=/etc" My goal is to transform the string into an array, then for each key, if it starts with "--proxy" to print the string... (2 Replies)
Discussion started by: TECK
2 Replies

5. Shell Programming and Scripting

Print unique names in each row of a specific column using awk

Is it possible to remove redundant names in the 4th column? input cqWE 100 200 singapore;singapore AZO 300 400 brazil;america;germany;ireland;germany .... .... output cqWE 100 200 singapore AZO 300 400 brazil;america;germany;ireland (4 Replies)
Discussion started by: quincyjones
4 Replies

6. Shell Programming and Scripting

Print unique names in a specific column using awk

Is it possible to modify file like this. 1. Remove all the duplicate names in a define column i.e 4th col 2. Count the no.of unique names separated by ";" and print as a 5th col thanx in advance!! Q input c1 30 3 Eh2 c10 96 3 Frp c41 396 3 Ua5;Lop;Kol;Kol c62 2 30 Fmp;Fmp;Fmp ... (5 Replies)
Discussion started by: quincyjones
5 Replies

7. Shell Programming and Scripting

Delete column with exact string in specific col

Hi, my file structur looks like File structure looks: GeneID protein_gi Symbol 1246500 10954455 repA1 1246501 10954457 repA2 1246502 10954458 leuA But some of the cases do not have record for protein id. for example: 1343044 - orf01 I want to remove those rows. But I tried awk... (9 Replies)
Discussion started by: smitra
9 Replies

8. Shell Programming and Scripting

Replace specific column range in a non-delimited file with a string!

Hi All, I will need an help with respect to replacing a range of columns on a non-delimited file using a particular string pattern. Say file input is MYNUMBERD000000-BAN CHUE INSNTS ** N+ MYAREDSDD000000+BAN CHUE INSNTS ** N+ MYDERFFFSD00000-GIR PENT - ACH ** ... (5 Replies)
Discussion started by: navojit dutta
5 Replies

9. Shell Programming and Scripting

Overwrite specific column in xml file with the specific column from adjacent line

I have an xml file dumped from rrd file, that I want to "patch" so the xml file doesn't contain any blank hole in the resulting graph of the rrd file. Here is the file. <!-- 2015-10-12 14:00:00 WIB / 1444633200 --> <row><v> 4.0419731265e+07 </v><v> 4.5045912770e+06... (2 Replies)
Discussion started by: rk4k
2 Replies

10. Shell Programming and Scripting

Change some string in specific column with space

Hello All of Master Script , i need help to solve my problem before : mount /dev/rdsk/c1t69d0s6 /dev/rdsk/c1t69d0s6 /vol/cl123/PURGE1 ufs mount /dev/rdsk/c1t70d0s6 /dev/rdsk/c1t70d0s6 /vol/cl123/PURGE2 ufs expected : mount /dev/dsk/c1t69d0s6 /dev/rdsk/c1t69d0s6 /PURGE1 ufs mount ... (3 Replies)
Discussion started by: k0p0nkkk
3 Replies
DBIx::Class::Manual::Joining(3) 			User Contributed Perl Documentation			   DBIx::Class::Manual::Joining(3)

NAME
DBIx::Class::Manual::Joining - Manual on joining tables with DBIx::Class DESCRIPTION
This document should help you to use DBIx::Class if you are trying to convert your normal SQL queries into DBIx::Class based queries, if you use joins extensively (and also probably if you don't). WHAT ARE JOINS
If you ended up here and you don't actually know what joins are yet, then you should likely try the DBIx::Class::Manual::Intro instead. Skip this part if you know what joins are.. But I'll explain anyway. Assuming you have created your database in a more or less sensible way, you will end up with several tables that contain "related" information. For example, you may have a table containing information about "CD"s, containing the CD title and it's year of publication, and another table containing all the "Track"s for the CDs, one track per row. When you wish to extract information about a particular CD and all it's tracks, You can either fetch the CD row, then make another query to fetch the tracks, or you can use a join. Compare: SELECT ID, Title, Year FROM CD WHERE Title = 'Funky CD'; # .. Extract the ID, which is 10 SELECT Name, Artist FROM Tracks WHERE CDID = 10; SELECT cd.ID, cd.Title, cd.Year, tracks.Name, tracks.Artist FROM CD JOIN Tracks ON CD.ID = tracks.CDID WHERE cd.Title = 'Funky CD'; So, joins are a way of extending simple select statements to include fields from other, related, tables. There are various types of joins, depending on which combination of the data you wish to retrieve, see MySQL's doc on JOINs: <http://dev.mysql.com/doc/refman/5.0/en/join.html>. DEFINING JOINS AND RELATIONSHIPS
In DBIx::Class each relationship between two tables needs to first be defined in the ResultSource for the table. If the relationship needs to be accessed in both directions (i.e. Fetch all tracks of a CD, and fetch the CD data for a Track), then it needs to be defined for both tables. For the CDs/Tracks example, that means writing, in "MySchema::CD": MySchema::CD->has_many('tracks', 'MySchema::Tracks'); And in "MySchema::Tracks": MySchema::Tracks->belongs_to('cd', 'MySchema::CD', 'CDID'); There are several other types of relationships, they are more comprehensively described in DBIx::Class::Relationship. USING JOINS
Once you have defined all your relationships, using them in actual joins is fairly simple. The type of relationship that you chose e.g. "has_many", already indicates what sort of join will be performed. "has_many" produces a "LEFT JOIN" for example, which will fetch all the rows on the left side, whether there are matching rows on the right (table being joined to), or not. You can force other types of joins in your relationship, see the DBIx::Class::Relationship docs. When performing either a search or a find operation, you can specify which "relations" to also refine your results based on, using the join attribute, like this: $schema->resultset('CD')->search( { 'Title' => 'Funky CD', 'tracks.Name' => { like => 'T%' } }, { join => 'tracks', order_by => ['tracks.id'], } ); If you don't recognise most of this syntax, you should probably go read "search" in DBIx::Class::ResultSet and "ATTRIBUTES" in DBIx::Class::ResultSet, but here's a quick break down: The first argument to search is a hashref of the WHERE attributes, in this case a restriction on the Title column in the CD table, and a restriction on the name of the track in the Tracks table, but ONLY for tracks actually related to the chosen CD(s). The second argument is a hashref of attributes to the search, the results will be returned sorted by the "id" of the related tracks. The special 'join' attribute specifies which "relationships" to include in the query. The distinction between "relationships" and "tables" is important here, only the "relationship" names are valid. This slightly nonsense example will produce SQL similar to: SELECT cd.ID, cd.Title, cd.Year FROM CD cd JOIN Tracks tracks ON cd.ID = tracks.CDID WHERE cd.Title = 'Funky CD' AND tracks.Name LIKE 'T%' ORDER BY 'tracks.id'; FETCHING RELATED DATA
Another common use for joining to related tables, is to fetch the data from both tables in one query, preventing extra round-trips to the database. See the example above in "WHAT ARE JOINS". Three techniques are described here. Of the three, only the "prefetch" technique will deal sanely with fetching related objects over a "has_many" relation. The others work fine for 1 to 1 type relationships. Whole related objects To fetch entire related objects, e.g. CDs and all Track data, use the 'prefetch' attribute: $schema->resultset('CD')->search( { 'Title' => 'Funky CD', }, { prefetch => 'tracks', order_by => ['tracks.id'], } ); This will produce SQL similar to the following: SELECT cd.ID, cd.Title, cd.Year, tracks.id, tracks.Name, tracks.Artist FROM CD JOIN Tracks ON CD.ID = tracks.CDID WHERE cd.Title = 'Funky CD' ORDER BY 'tracks.id'; The syntax of 'prefetch' is the same as 'join' and implies the joining, so there is no need to use both together. Subset of related fields To fetch a subset or the related fields, the '+select' and '+as' attributes can be used. For example, if the CD data is required and just the track name from the Tracks table: $schema->resultset('CD')->search( { 'Title' => 'Funky CD', }, { join => 'tracks', '+select' => ['tracks.Name'], '+as' => ['track_name'], order_by => ['tracks.id'], } ); Which will produce the query: SELECT cd.ID, cd.Title, cd.Year, tracks.Name FROM CD JOIN Tracks ON CD.ID = tracks.CDID WHERE cd.Title = 'Funky CD' ORDER BY 'tracks.id'; Note that the '+as' does not produce an SQL 'AS' keyword in the output, see the DBIx::Class::Manual::FAQ for an explanation. This type of column restriction has a downside, the resulting $row object will have no 'track_name' accessor: while(my $row = $search_rs->next) { print $row->track_name; ## ERROR } Instead "get_column" must be used: while(my $row = $search_rs->next) { print $row->get_column('track_name'); ## WORKS } Incomplete related objects In rare circumstances, you may also wish to fetch related data as incomplete objects. The usual reason to do is when the related table has a very large field you don't need for the current data output. This is better solved by storing that field in a separate table which you only join to when needed. To fetch an incomplete related object, supply the dotted notation to the '+as' attribute: $schema->resultset('CD')->search( { 'Title' => 'Funky CD', }, { join => 'tracks', '+select' => ['tracks.Name'], '+as' => ['tracks.Name'], order_by => ['tracks.id'], } ); Which will produce same query as above; SELECT cd.ID, cd.Title, cd.Year, tracks.Name FROM CD JOIN Tracks ON CD.ID = tracks.CDID WHERE cd.Title = 'Funky CD' ORDER BY 'tracks.id'; Now you can access the result using the relationship accessor: while(my $row = $search_rs->next) { print $row->tracks->name; ## WORKS } However, this will produce broken objects. If the tracks id column is not fetched, the object will not be usable for any operation other than reading its data. Use the "Whole related objects" method as much as possible to avoid confusion in your code later. Broken means: Update will not work. Fetching other related objects will not work. Deleting the object will not work. COMPLEX JOINS AND STUFF
Across multiple relations For simplicity in the example above, the "Artist" was shown as a simple text field in the "Tracks" table, in reality, you'll want to have the artists in their own table as well, thus to fetch the complete set of data we'll need to join to the Artist table too. In "MySchema::Tracks": MySchema::Tracks->belongs_to('artist', 'MySchema::Artist', 'ArtistID'); The search: $schema->resultset('CD')->search( { 'Title' => 'Funky CD' }, { join => { 'tracks' => 'artist' }, } ); Which is: SELECT me.ID, me.Title, me.Year FROM CD me JOIN Tracks tracks ON CD.ID = tracks.CDID JOIN Artists artist ON tracks.ArtistID = artist.ID WHERE me.Title = 'Funky CD'; To perform joins using relations of the tables you are joining to, use a hashref to indicate the join depth. This can theoretically go as deep as you like (warning: contrived examples!): join => { room => { table => 'leg' } } To join two relations at the same level, use an arrayref instead: join => { room => [ 'chair', 'table' ] } Or combine the two: join => { room => [ 'chair', { table => 'leg' } ] Table aliases As an aside to all the discussion on joins, note that DBIx::Class uses the "relation names" as table aliases. This is important when you need to add grouping or ordering to your queries: $schema->resultset('CD')->search( { 'Title' => 'Funky CD' }, { join => { 'tracks' => 'artist' }, order_by => [ 'tracks.Name', 'artist.Artist' ], } ); SELECT me.ID, me.Title, me.Year FROM CD me JOIN Tracks tracks ON CD.ID = tracks.CDID JOIN Artists artist ON tracks.ArtistID = artist.ID WHERE me.Title = 'Funky CD' ORDER BY tracks.Name, artist.Artist; This is essential if any of your tables have columns with the same names. Note that the table of the resultsource the search was performed on, is always aliased to "me". Joining to the same table twice There is no magic to this, just do it. The table aliases will automatically be numbered: join => [ 'room', 'room' ] The aliases are: "room" and "room_2". perl v5.16.2 2012-08-16 DBIx::Class::Manual::Joining(3)
All times are GMT -4. The time now is 02:51 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy