bash assign mysql query single field to variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting bash assign mysql query single field to variable
# 1  
Old 05-11-2010
bash assign mysql query single field to variable

I'm running a bash script query and assigning the output to a variable like this:
Code:
exists=`mysql -u $USER_NAME --password=$PASSWORD -D "somedb" \
           -e "SELECT * FROM somedb.sometable WHERE field1 ='$a' \
           AND field2 ='$b' LIMIT 0 , 30";`

which returns something like:
Code:
echo $exists

id field1 field2 field3 field4 field5

if field1 = $a and field2 = $b. I want to assign only one field from that output to a variable so
Code:
c=field3

I was using awk, but if field 3 is more than one word then awk thinks that's 2 fields and screws up my variable.
# 2  
Old 05-11-2010
Why are you doing 'SELECT *'?

Why not 'SELECT field3'?
This User Gave Thanks to soleil4716 For This Post:
# 3  
Old 05-12-2010
wow, that was profoundly obvious Smilie I can't believe I didn't see that after getting this far, I need sleep! I appreciate another set of eyes looking at it, thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Variable definition inside mysql query

Hello, I am running ubuntu16.04. By means of @Rudic's help, I have below command. What I need to do is to replace video_id by value of video_id in which WHERE clause is matched: {print "INSERT INTO video_series_files (id, video_id, file_type, protocol, \ url, languages, quality, accessed... (12 Replies)
Discussion started by: baris35
12 Replies

2. Shell Programming and Scripting

How to Assign the Output of an SQL Query to a Variable?

Hi iam new to shell scripting how to declare variables as redshift query and I have to compare two counts by using if condition . ex:count=select count(*) from prd; select count(*) from prd; select count(*) from tag; can any one help me . Please use CODE tags when displaying... (1 Reply)
Discussion started by: sam526
1 Replies

3. Shell Programming and Scripting

Conditional bash/mysql query help

I think(hope) I've got a simple one - I just need to send an email if a mysql query returns any results (ideally - it will never match). Currently I just pipe the mysql query output to the mail program, but of course that emails regardless of the output( and I check this every 10 minutes from... (5 Replies)
Discussion started by: jcass78
5 Replies

4. Shell Programming and Scripting

Storing the value of a single query to a Variable - What am i doing wrong?

I tried the below : #!/bin/ksh testvar=$( sqlplus $user/$pass << EOF set pagesize 0 feedback off verify off heading off echo off; whenever sqlerror exit 1 SELECT 1+1 FROM DUAl; COMMIT; EXIT; EOF) echo "testvar="$testvar The output that I am getting is : > sh example.sh... (3 Replies)
Discussion started by: SriRamKrish
3 Replies

5. Programming

mysql query multiple records for one field

Hello Group, What I have is a database with about a dozen fields and one being "City". What I would like to do is to have a custom query on a single field for multiple items (cities) but I don't know how to do this. I know this is probably kids play for most of you but I am lost. What I have... (4 Replies)
Discussion started by: vestport
4 Replies

6. Shell Programming and Scripting

Using a shell script variable in a mysql query using 'LIKE'

Hello. I am writing a simple script that reads a text file and removes records from a mysql database. The items in the text file are of the format: firstname.middle.lastXXX, where XXX is a 3 digit number. The table has an email field that will match the firstname.middle.last. So, I thought I... (1 Reply)
Discussion started by: bricoleur
1 Replies

7. Shell Programming and Scripting

Assign field value in a .csv file to a variable using ksh

Hi, I'm new to the scripting world... I want to know that how can I assign the the field value(that has multiple lines) of a .csv file to a variable??? Model of my .csv file : 1 Poppy 5 2 red 6 3 black 5 4 white 8 and so on,the list... (4 Replies)
Discussion started by: srim
4 Replies

8. Shell Programming and Scripting

Perl:Read single value from text file and assign to variable

Hello All, A part of my very basic perl code requires me to read a single value from a text file. The file output is the following: Reading image ... done IMAGEREGION=0x0x0-256x162x256 VOXELDIMENSION=0.9375000000x1.2000000477x0.9375000000 VOXELNUMBER=10527001... (7 Replies)
Discussion started by: ncl
7 Replies

9. Shell Programming and Scripting

Passing a variable from shell script to mysql query?

I heard this was possible but from my research I haven't been able to figure it out yet. Seems it should be simple enough. Basically from a high level view I'm trying to accomplish... . $X='grep foo blah.log' then 'mysql command SELECT foo FROM bar WHERE ' . $X or something like that. ... (2 Replies)
Discussion started by: kero
2 Replies

10. Shell Programming and Scripting

Assign bash command to variable

Hi I am trying to write a function that needs to be able to assign the last run shell command to a variable. The actual command string itself not the exit code of the command. I am using the bash command recall ability to do this as follows: alias pb='ps | grep ash' ... (3 Replies)
Discussion started by: Moxy
3 Replies
Login or Register to Ask a Question