Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

MYSQL - left join problem

Posted on 2011-02-22
6
Medium Priority
?
433 Views
Last Modified: 2012-05-11
Hi,

I am trying to create an application in php to create an SQL join, I can code the application, but I am having problems creating the string:

the SQL syntax is where the problem is: the SQL I have is:

SELECT id, username, password, rank FROM TESTTABLE1 LEFT JOIN TESTTABLE2 ON TESTTABLE1.id = TESTTABLE2.id WHERE TESTTABLE2.id > 0

the table structure is as follows:

TESTTABLE1
id username password status   rank
1  gavin    12345bbc limited  low
2  david    456456fg limited  low

TESTTABLE2
id username
1  gavin
2  david

please can someone explain where it is that I am going wrong, thanks

Gavin
0
Comment
Question by:GPB1983
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 4

Expert Comment

by:ziycon
ID: 34951315
Try something like:

SELECT tt1.id, tt1.username, tt1.password, tt1.rank FROM testtable1 tt1, testtable2 tt2 WHERE tt1.id = tt2.id AND tt2.id > 0;
0
 

Author Comment

by:GPB1983
ID: 34951425
hiya, thanks for the quick reply,

I have tried your solution and it generates nothing what so ever.

the syntax I have used is:
SELECT TESTTABLE1.id, TESTTABLE1.username, TESTTABLE1.password, TESTTABLE1.rank FROM TESTTABLE1, TESTTABLE2 WHERE TESTTABLE1.id = TESTTABLE2.id AND tt2.id

and the DB engine is InnoDB (supports foreign keys)

where am I going wrong please?

regards, Gavin
0
 
LVL 4

Accepted Solution

by:
ziycon earned 2000 total points
ID: 34951488
I'm using the below query but both this one and the first one work grand form me with the tables you have given before on mySQL.

SELECT tt1.id, tt1.username, tt1.password, tt1.rank FROM testtable1 tt1, testtable2 tt2 WHERE tt1.id = tt2.id AND tt2.id <> 0;

How are you running these queries?
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

Author Comment

by:GPB1983
ID: 34951567
Hi, thanks it works, well sort of:

it gives me some results, but only the contents of testtable1, I thought that this query would basically give me the contents of both tables.

thanks,

regards, Gavin
0
 

Author Comment

by:GPB1983
ID: 34951636
false alarm mate, I saw where I was going wrong.

thanks for the help, EE needs more people like you!

top marks all round for ya!

regards, Gavin
0
 

Author Closing Comment

by:GPB1983
ID: 34951792
top marks, straight tot the point, EE needs more like him
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
In this article, we’ll look at how to deploy ProxySQL.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question