Visit Our UNIX and Linux User Community

mysql query for multiple columns from multiple tables in a DB

Thread Tools Search this Thread
Top Forums Web Development mysql query for multiple columns from multiple tables in a DB
# 1  
Old 07-07-2011
mysql query for multiple columns from multiple tables in a DB

Say I have two tables like below..

HId	sName	dName	StartTime	EndTime
1	E	        E	        9:10	        10:10
2	E	        F	        9:15	        10:15
3	G	        H	        9:17	        10:00

Id	devName	capacity	free	Line
1	E	        123	        34	1
2	E	        345	        45	1
3	G	        345	        23	0

status.HId and logical.Id denote the same value that is a id of a server
I'm trying to write a query to retrieve for a few columns in each table where logic.Line=1;

select HId, sName, dName, StartTime, capacity, free from status s, logical l WHERE s.HId=2 and l.Line=1;

expecting the output to be.. but the above query gives multiple lines of output..

HId	sName	dName	StartTime capacity	free
2	E	        F	        9:15        345	        45

i'm not sure if we can achieve with mysql joins here but want to take advice while i can read and do the above..

Last edited by pludi; 07-07-2011 at 06:02 AM..
# 2  
Old 07-07-2011
It just smushes them together indiscriminately because, when two tables have nothing to do with each other, it's got no way to know which rows are relevant and which aren't. The rows aren't strictly guaranteed to be in any particular order; to be consistent it has to consider all possibilities.

Or do the tables have anything in common you'd like to match on?
# 3  
Old 07-07-2011
yes the tables have HId and Id in common.
# 4  
Old 07-07-2011
sql easily supports that, but you need to tell it what things to join on.
SELECT ... FROM s LEFT JOIN local ON (s.hid = WHERE ...

You may need to tell it local.columname instead of just columnname for columns in local.

Previous Thread | Next Thread
Test Your Knowledge in Computers #610
Difficulty: Medium
Compared to Python, Perl has a relatively small Comprehensive Perl Archive Network (CPAN) library.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Export Oracle multiple tables to multiple csv files using UNIX shell scripting

Hello All, just wanted to export multiple tables from oracle sql using unix shell script to csv file and the below code is exporting only the first table. Can you please suggest why? or any better idea? export FILE="/abc/autom/file/geo_JOB.csv" Export= `sqlplus -s dev01/password@dEV3... (16 Replies)
Discussion started by: Hope
16 Replies

2. UNIX for Beginners Questions & Answers

Using bash script : How to Import data from a dsv file into multiple tables in mysql

HI I have a dsv file that looks like: <<BOF>> record_number|id_number|first name|last name|msisdn|network|points|card number|gender 312|9101011234011|Test Junior|Smith|071 123 4321|MTN|73|1241551413214444|M 313|9012023213011|Bob|Smith|27743334321|Vodacom|3|1231233232323244|M... (4 Replies)
Discussion started by: tera
4 Replies

3. Shell Programming and Scripting

Shell script automation using cron which query's MySQL Tables

What I have: I have a (script which basically connect to mysql-db and query's multiple tables to write back the output to output1.out file in a directory) note: I need to pass an integer (unique_id = anything b/w 1- 1000) next to the script everytime I run the script which generates... (3 Replies)
Discussion started by: kkpand
3 Replies

4. Shell Programming and Scripting

Removing carriage returns from multiple lines in multiple files of different number of columns

Hello Gurus, I have a multiple pipe separated files which have records going over multiple Lines. End of line separator is \n and records going over multiple lines have <CR> as separator. below is example from one file. 1|ABC DEF|100|10 2|PQ RS T|200|20 3| UVWXYZ|300|30 4| GHIJKL|400|40... (7 Replies)
Discussion started by: dJHa
7 Replies

5. Shell Programming and Scripting

Assigning multiple column's value from Oracle query to multiple variables in UNIX

Hi All, I need to read values of 10 columns from oracle query and assign the same to 10 unix variables. The query will return only one record(row). I tried to append all these columns using a delimiter(;) in the select query and assign the same to a single variable(V) in unix. I thought I... (3 Replies)
Discussion started by: hkrishnan91
3 Replies

6. Shell Programming and Scripting

UPDATE COmmand post comparing 2 columns in 2 mysql tables

my queryis : select distinct, item_count, item from master m join client p on where item_count = 1 and item > 1; But how should I update them? i used update statetment : Update from client Set item =1 where and item_count=1 AND item>1 Is this wrong? (1 Reply)
Discussion started by: siya@
1 Replies

7. Shell Programming and Scripting

Reading multiple values from multiple lines and columns and setting them to unique variables.

Hello, I would like to ask for help with csh script. An example of an input in .txt file is below, the number of lines varies from file to file and I have 2 or 3 columns with values. I would like to read all the values (probably one by one) and set them to independent unique variables that... (7 Replies)
Discussion started by: FMMOLA
7 Replies

8. Shell Programming and Scripting

Compare multiple files with multiple number of columns

Hi, input file1 abcd 123 198 xyz1:0909090-0909091 ghij 234 999 xyz2:987654:987655 kilo 7890 7990 xyz3:12345-12357 prem 9 112 xyz5:97-1134 input file2 abcd 123 198 xyz1:0909090-0909091 -9.122 0 abed 88 98 xyz1:98989-090808 -1.234 1.345 ghij 234 999 xyz2:987654:987655 -10.87090909 5... (5 Replies)
Discussion started by: jacobs.smith
5 Replies

9. Shell Programming and Scripting

Awk match multiple columns in multiple lines in single file

Hi, Input 7488 7389 chr1.fa chr1.fa 3546 9887 chr5.fa chr9.fa 7387 7898 chrX.fa chr3.fa 7488 7389 chr21.fa chr3.fa 7488 7389 chr1.fa chr1.fa 3546 9887 chr9.fa chr5.fa 7898 7387 chrX.fa chr3.fa Desired Output 7488 7389 chr1.fa chr1.fa 2 3546 9887 chr5.fa chr9.fa 2... (2 Replies)
Discussion started by: jacobs.smith
2 Replies

10. 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

Featured Tech Videos