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

MySQL query

42
Hi
I can't get this table join to work

lets say I have 2 tables

table 1

Expand|Select|Wrap|Line Numbers
  1. ID NAME
  2. 1  S1
  3. 2  S2
  4.  
table 2

Expand|Select|Wrap|Line Numbers
  1. ID TIMECREATED ACTIVE
  2. 1   time                    1
  3. 2   time                    0
  4. 1   time                    1
  5. 1   time                    0
  6. 2   time                    1
  7.  
What I want to query is "S1" and "S2" and then join the table 2 like "ON table1.id=table2.id" but I want only the latest value (1 record) created (ORDER by TIMECREATED , LIMIT 1) for that ID join the first table and if it is also possible to check for table2.ACTIVE , even better.

I can't seem to work it out :(
Jan 23 '08 #1
7 1193
mwasif
802 Expert 512MB
Try this
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM  
  2.  (
  3.    SELECT * FROM table2 WHERE ACTIVE=1 
  4.    ORDER BY TIMECREATED DESC
  5.  ) as tmp
  6. INNER JOIN table1 ON table1.ID = tmp.ID
  7. GROUP BY table1.NAME
Jan 23 '08 #2
xhunter
42
Try this
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM  
  2.  (
  3.    SELECT * FROM table2 WHERE ACTIVE=1 
  4.    ORDER BY TIMECREATED DESC
  5.  ) as tmp
  6. INNER JOIN table1 ON table1.ID = tmp.ID
  7. GROUP BY table1.NAME
hmm.....

that makes things a little complicated in my scripts going around and joining table 1 instead..
I am gonna try to make it out, hope it works

by the way, does that make this "2" queries on the database ?, I am trying to get the number of my queries as minimal as possible,
Jan 23 '08 #3
xhunter
42
OK,

I think there is a problem I didn't think the first time.

actually my first table is a list of jobs and the 2nd is the list of logs,

so there actually could be no records in the logs for the jobs in the first table, so joining the table1 to 2 would skip the records with no log.

i got my hopes up for sec..... :/
Jan 23 '08 #4
mwasif
802 Expert 512MB
In this case you should use LEFT JOIN in this way
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM jobs 
  2. LEFT JOIN 
  3.  (
  4.    SELECT * FROM logs WHERE ACTIVE=1 
  5.    ORDER BY TIMECREATED DESC
  6.  ) as tmp_logs ON tmp_logs.id = jobs.id
  7. GROUP BY jobs.NAME
This query creates a temporary table for LEFT JOIN.

This code is not tested.
Jan 23 '08 #5
xhunter
42
thanx,

when I saw u using 2 select statements, that's when what I immediately thought to use but I ran into 1 little problem, I need to get a value from the first select to use in the 2nd one, I could get it

Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM jobs d
  2. LEFT OUTER JOIN (SELECT * FROM logs WHERE d.id = cjobid ORDER BY jobid DESC LIMIT 1) f
  3. ON d.id = f.cjobid
  4.  
as you would see, "d.id" it's not known in the 2nd select
Jan 23 '08 #6
mwasif
802 Expert 512MB
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM jobs d
  2. LEFT OUTER JOIN (SELECT * FROM logs ORDER BY jobid DESC LIMIT 1) f
  3. ON d.id = f.cjobid
You have d.id = cjobid in the JOIN.
Jan 23 '08 #7
xhunter
42
thanx alot,

I ended up using the "GROUP BY" instead, as the jobid was different for different jobs,

but anyways it works now they way I want it,

thanks again for ur help, I didn't think of using a 2nd select :)
Jan 23 '08 #8

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

Similar topics

0
by: Lenz Grimmer | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, MySQL 4.0.14, a new version of the popular Open Source/Free Software Database, has been released. It is now available in source and binary...
0
by: Mike Chirico | last post by:
Interesting Things to Know about MySQL Mike Chirico (mchirico@users.sourceforge.net) Copyright (GPU Free Documentation License) 2004 Last Updated: Mon Jun 7 10:37:28 EDT 2004 The latest...
1
by: Cern | last post by:
Is it somebody out there who has made a migration from an Oracle server to an MySQL server?? The scenario is as simply: I've got a Oracle 8 server with a database with content that I want to...
1
by: jlee | last post by:
I'm pretty much a newbie on mysql, and I need some help. I am running mysql Ver 12.22 Distrib 4.0.24, for portbld-freebsd5.4 (i386) on a server hosting an active website. The site's developer...
1
by: Good Man | last post by:
Hi there I've noticed some very weird things happening with my current MySQL setup on my XP Laptop, a development machine. For a while, I have been trying to get the MySQL cache to work....
3
by: Juan Antonio Villa | last post by:
Hello, I'm having a problem replicating a simple database using the binary log replication, here is the problem: When the master sends an update to the slave, an example update reads as follows:...
1
by: Ike | last post by:
Recently, I began using a different MySQL verver (i.e. different machine as well as different version#, going from 4.12a to 4.1.9 max). The following query used to work: select firstname,...
3
by: Me Alone | last post by:
Hello: I am trying to edit some C code I found in "The definitive guide to using, programming, and administering MySQL" by Paul DuBois. This C client program connects and then segfaults when...
221
Atli
by: Atli | last post by:
You may be wondering why you would want to put your files “into” the database, rather than just onto the file-system. Well, most of the time, you wouldn’t. In situations where your PHP application...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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...

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.