473,418 Members | 2,174 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,418 software developers and data experts.

a question about sql join

hello sir,

i am a fresh php developer , now i face a problem in sql queries (i only know a little about sql)
my table model is like this

TABLE1 NAME -:EMPLOYEE

ename eid dept

RAM 001 HR
HARI 002 software


TABLE 2 NAME:- ADMIN

username password eid
HR secureyes 001

Expand|Select|Wrap|Line Numbers
  1. select * from employee as e left join admin as a on a.eid=e.eid
i got a resultant table where eid comes twice

ename eid dept username password eid
RAM 001 HR admin secureyes 001
hari 002 software null null 002
1.how to eliminate this second eid from resultant table.

2.is there anyway to name the this above resultant table
3.i want to retrive all details of "hari" using his id and ram's password as condition ..both conditions are necessary for security purpose


hope for early reply ....
thanks and with regards Bikash
Sep 22 '10 #1
4 1089
gpl
152 100+
Hi
YES, this is easily done, * in this context means ALL fields (columns).

SELECT takes a comma separated list of fields, so try this
Expand|Select|Wrap|Line Numbers
  1. SELECT e.ename, 
  2.        e.eid, 
  3.        e.dept, 
  4.        a.username, 
  5.        a.password 
  6. FROM   employee AS e 
  7.        LEFT JOIN admin AS a 
  8.          ON a.eid = e.eid
you see that I am prefixing the column name with the table alias, so (in the case of eid), the parser knows which eid we want.

Im not sure what you are asking in the second point above, could you explain
And the third point, you seem to be relating 2 rows, for 1 row, you would add a WHERE clause, something like this
Expand|Select|Wrap|Line Numbers
  1. WHERE a.username = 'username' AND
  2.             a.password = 'password'
  3.  
where the username and password strings are passed in as parameters

good luck
Graham
Sep 22 '10 #2
thanks sir
i got the solution for my question no 1

but as per my second question i want to use that table created by joining of two tables as a sub query

like this
[code]select * from (select e.eid,e.ename,e.dept,a.username from employee as e left join admin as a on a.eid=e.eid) where e.eid=002[\code]

is this a workable code
Sep 23 '10 #3
gpl
152 100+
Very nearly
Expand|Select|Wrap|Line Numbers
  1. SELECT * 
  2. FROM   (SELECT e.eid, 
  3.                e.ename, 
  4.                e.dept, 
  5.                a.username 
  6.         FROM   employee AS e 
  7.                LEFT JOIN admin AS a 
  8.                  ON a.eid = e.eid) x
  9. WHERE  x.eid = 002  
I think you have to provide an alias for the derived table
Sep 23 '10 #4
ck9663
2,878 Expert 2GB
Or simply...

Expand|Select|Wrap|Line Numbers
  1.  
  2. select e.eid,e.ename,e.dept, a.username 
  3. from employee as e 
  4. left join admin as a on a.eid=e.eid
  5. where e.eid=002
  6.  
  7.  
But if you will use the result to join into another, GPL is right. Just be careful on using too much subquery...

Happy Coding!!!

~~ CK
Sep 23 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: B. Fongo | last post by:
I learned MySQL last year without putting it into action; that is why I face trouble in formulating my queries. Were it a test, then you would have passed it, because your queries did help me...
3
by: K Finegan | last post by:
Hello All, I'm trying to find out exactly what JOIN does eg. SELECT A.Name FROM Author A JOIN Publisher P ON A.SomeID = P.SomeID WHERE P.Country = 'X'
11
by: Mahmood | last post by:
what is the syntax to join a table with the result of antoher query. For example i have two tables Create Table Customers ( CustomerID int, LastPaymentDate Datetime ) Create Table...
4
by: PASQUALE | last post by:
Hi I have a question: do the both statements below give the same result? If yes then does somebody know something about preformance differencies using these joins? SELECT A.* FROM Table1 A...
3
by: mariohiga | last post by:
Hello I've a really big doubt :) I've this two alternatives SELECT * FROM T1 a INNER JOIN T1 b ON a.F1 = b.F1 AND a.F2 = 1 SELECT *
4
by: nephish | last post by:
i have seen the join method before, mostly in this group and i want to get it a little better. if i have a list x = how can i make it print as a single string? or make a single string out...
1
by: Preeti.s83 | last post by:
is it possible to replace join type ( for eg. nested loop with hash join and so on) in xml plan... we will fst take plan in xml format ( show xml plan ) and then we will replace join with other...
2
by: shenliang1985 | last post by:
I have Created two tables to test join key in sql server,but the result is not what I thougt.Why?Wish all your explains.Thanks.I give all the sql code in comment. --The table name is first for...
3
by: rengaraj | last post by:
hi frens I want to know some information/ explaination from SQL.. I want to know about join queries, subquries, timestamps, date_part, date_trunc, case and cast data type.. Do anyone...
3
by: zensunni | last post by:
Say I have a database like so: USER user_id user_name REPORT report_id report_name
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.