473,387 Members | 1,789 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

select from 2 tbl where feild is comman

Hi,
I am confused with this query I have to write a query that will select
data from many tables having a username field where username is given.

I used something like,

select * from tbl1,tbl2
where username = 'user'

but it gives a result with all the fields of one to the second table.

what to do, if table1 has 5 records with the given user and table 2
has 10 then I want only 15 results but the script is giving me lots of
results.

Please currect my query, I have to deal in 8 tables and I am having
problem in 2.

regards
Jaunty Edward
Jul 20 '05 #1
3 1763
Jaunty Edward wrote:
Hi,
I am confused with this query I have to write a query that will select
data from many tables having a username field where username is given.

I used something like,

select * from tbl1,tbl2
where username = 'user'


Normally you have something like this:

------------
| tbl1 |
------------
| id |
| username |
------------

------------
| tbl2 |
------------
| id |
| tbl1_id |
| some |
| other |
| fields |
------------

tbl1.id is linked to tbl2.id, so you want to select one row from tbl1
and all rows from tbl2 that match to that:

select * from tbl1,tbl2
where tbl1.username = 'user'
and tbl1.id = tbl2.tbl1_id;
Jul 20 '05 #2
Thanks Aggro,
but I don't have any feild such as tbl1_id in the table2. Please look
at my DB structure for a better picture.

CREATE TABLE `dating` (
`id` int(5) unsigned NOT NULL auto_increment,
`cat_id` tinyint(3) unsigned NOT NULL default '0',
`cat_name` varchar(60) NOT NULL default '',
`subcat_id` tinyint(3) unsigned NOT NULL default '0',
`sub_cat` varchar(60) NOT NULL default '',
`username` varchar(30) default NULL,
`sex` varchar(15) default NULL,
`age` tinyint(3) unsigned default NULL,
`email` varchar(30) default NULL,
`title` varchar(255) default NULL,
`message` text,
`date` date NOT NULL default '0000-00-00',
`time` time NOT NULL default '00:00:00',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=5 ;
CREATE TABLE `real_estate` (
`id` int(5) unsigned NOT NULL auto_increment,
`cat_id` tinyint(3) unsigned NOT NULL default '0',
`cat_name` varchar(60) NOT NULL default '',
`subcat_id` tinyint(3) unsigned NOT NULL default '0',
`sub_cat` varchar(60) NOT NULL default '',
`rent` varchar(30) default NULL,
`bhk` varchar(10) default NULL,
`username` varchar(30) default NULL,
`sex` varchar(15) default NULL,
`email` varchar(30) default NULL,
`msgheading` varchar(255) default NULL,
`message` text,
`price` varchar(10) NOT NULL default '0',
`address` varchar(60) NOT NULL default '',
`pincode` varchar(6) NOT NULL default '0',
`near_rlstation` varchar(30) NOT NULL default '',
`date` date NOT NULL default '0000-00-00',
`time` time NOT NULL default '00:00:00',
`sqfeet` int(10) unsigned default NULL,
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=21 ;

these are the 2 tables that have username and email comman.

Please help as I have to extract data from 7 such tables in one query.

Regards
Jaunty Edward
Jul 20 '05 #3
Jaunty Edward wrote:
Thanks Aggro,
but I don't have any feild such as tbl1_id in the table2. Please look
at my DB structure for a better picture.

CREATE TABLE `dating` (
`id` int(5) unsigned NOT NULL auto_increment,
`cat_id` tinyint(3) unsigned NOT NULL default '0',
`cat_name` varchar(60) NOT NULL default '',
`subcat_id` tinyint(3) unsigned NOT NULL default '0',
`sub_cat` varchar(60) NOT NULL default '',
`username` varchar(30) default NULL,
`sex` varchar(15) default NULL,
`age` tinyint(3) unsigned default NULL,
`email` varchar(30) default NULL,
`title` varchar(255) default NULL,
`message` text,
`date` date NOT NULL default '0000-00-00',
`time` time NOT NULL default '00:00:00',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=5 ;
CREATE TABLE `real_estate` (
`id` int(5) unsigned NOT NULL auto_increment,
`cat_id` tinyint(3) unsigned NOT NULL default '0',
`cat_name` varchar(60) NOT NULL default '',
`subcat_id` tinyint(3) unsigned NOT NULL default '0',
`sub_cat` varchar(60) NOT NULL default '',
`rent` varchar(30) default NULL,
`bhk` varchar(10) default NULL,
`username` varchar(30) default NULL,
`sex` varchar(15) default NULL,
`email` varchar(30) default NULL,
`msgheading` varchar(255) default NULL,
`message` text,
`price` varchar(10) NOT NULL default '0',
`address` varchar(60) NOT NULL default '',
`pincode` varchar(6) NOT NULL default '0',
`near_rlstation` varchar(30) NOT NULL default '',
`date` date NOT NULL default '0000-00-00',
`time` time NOT NULL default '00:00:00',
`sqfeet` int(10) unsigned default NULL,
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=21 ;

these are the 2 tables that have username and email comman.

Please help as I have to extract data from 7 such tables in one query.

Regards
Jaunty Edward


Just do it as

select dating.some_field,real_estate.some_field,... from
dating,real_estate,... where dating.username = "some_name" and
dating.username = real_estate.username and real_estate.username = ...

Basically, select all the fields you need from thier tables, match one
table's username to a given string, and then make sure the username
field in all tables is the same.
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: serge | last post by:
Using "SELECT * " is a bad practice even when using a VIEW instead of a table? I have some stored procedures that are identical with the difference of one statement in the WHERE clause. If I...
1
by: TARUN | last post by:
Hello All Please help me for understanding of use of Hidden Feild in Programming, I am a beginner of C#.NET, can anyone give me the idea about hidden feild like hidden textBox, hidden textArea...
10
by: Richard Gilmore | last post by:
I want the feild total to display the total cost of an item, If the item has vat a yes no box is ticked in the field vat and I have the price of the item without vat. I want the feild total to...
2
by: sathikbasha | last post by:
Hi friends, Please help me 1) I had insert three rows in a table called EMP with feild name EMP_NAME as "AA","BB","CC" (Oracle) in select query i am searching the name with "AA BB CC"...
5
by: jaemalegaon | last post by:
Hi , This Is Adarsh Jain Form Malegaon , A Remote Area In Dist Of Nashik, Maharastra(india), Wants Help In Arraning Data Record Of A Table According To Date Feild Of Data Record Of Same Table, I.e...
0
by: vidyamann | last post by:
Hello there, i have a problem with my php page. i have a form and different tabs on that. o the click of each tab, user will give his information in the text boxes. and for that i need a comman...
0
by: dgil2004 | last post by:
I am a real newbie when it comes to scripting, coding, etc We have a site were people go and load text documents. I can load the text document fine with no problems. But someone from another...
1
by: jeff8676 | last post by:
I'm very new at Access, so if this is a lame question I'm sorry. Anyways, I'm trying to make a query that returns unique entries from a table, but when it finds two entries where everything...
6
by: gavy7210 | last post by:
hello i am using struts 1.2,Eclipse Platform Version: 3.4.2,mySql 5.0.1,jdk 1.5.. i have a login form(jsp) in which a user logs in,in case he doesnt enter his username and/or password an error...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.