473,587 Members | 2,490 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ordering data by datetime from 2 different tables

33 New Member
ok, so, what im trying to do is complicated for me, im trying to do this with as least amount of server usage as possible... what im trying to do is basically make an output that shows my "friends updates" but in time order.
Expand|Select|Wrap|Line Numbers
  1. $sqlF = mysql_query("SELECT * FROM myMembers WHERE id=6 ");
  2. while($rows = mysql_fetch_array($sqlF)){
  3.     $friend_array = $rows["friend_array"];
  4. }
  5. $friend_array = explode(",", $friend_array);
  6.  
  7. foreach($friend_array as $key => $value){
  8.     $sql = mysql_query("SELECT * FROM updates WHERE user=$value");
  9.     while($rowin = mysql_fetch_array($sql)){
  10.         $name .= ''.$rowin["datetime"].'<br />';
  11.     }
  12. }
  13.  
the code above is almost right, it shows the list and orders them ascending, but it does them by each friend. example

id date friend number
4 2012-05-22 19:15:07 <= friend #4
5 2012-05-22 21:28:58 <= friend #4
1 2012-05-22 16:51:22 <= friend #8
2 2012-05-22 16:56:24 <= friend #8
3 2012-05-22 16:56:49 <= friend #8

and its good but not quite all the way there, what im wanting to do is have it order the whole output by the date/time and not by friend order... right now as you can tell the output is listed by friend order from the exploded freinds list, and not by time. should i do a LEFT JOIN in there to save a query and would that help with ordering by the date/time? any directions would be great, thanks
gt
May 26 '12 #1
16 2560
Rabbit
12,516 Recognized Expert Moderator MVP
Join the members table to the update table.
May 27 '12 #2
manohoo
2 New Member
You are correct, a joined query is the answer. However, you don't provide the tables structures, so you leave us in the dark. It would look something like this:
Expand|Select|Wrap|Line Numbers
  1. SELECT * 
  2. FROM myMembers, updates 
  3. WHERE myMembers.? = updates.? (the joined columns)
  4. AND myMembers.id=6 
  5. ORDER BY updates.datetime");

A less elegant solution would be to sort the array.
May 27 '12 #3
George Thompson
33 New Member
sorry, i didnt leave more information, I was hoping to be able to do most of the work myself. but i havent had much luck, i will try to give more information... in the myMembers table i have a whole bunch of tables, only one i need from where is the ("friend_array" ) and from the updates table ill need all tables. ("id, user, name, datetime, type, profile_id, location_id, album_id, thread_id")

now what i think is the tricky part, is that in my "friends_ar ray" table, it has many people listed, for example ("4,2,7,12,8 ") thats how my freinds list looks and i think in the left join its not going to understand all the members are run together and only seperated by commas
May 27 '12 #4
Rabbit
12,516 Recognized Expert Moderator MVP
You should normalize your data. Then you won't have this problem. However, if absolutely necessary, you can join using the LIKE operator.
May 28 '12 #5
George Thompson
33 New Member
Rabbit, i dont understand what you mean by normalize my data... is there anyway i can get a sample senario? i hate asking for things to be handed to me, but this is the last piece of the puzzle and im going on day 3 for this part, searching online for what im trying to accomplish. it has to be doable, i just cant get my hands around it.

no matter how i try to put it in the query, it just keeps out putting the data from "updates" table in order of the "friends_array" . and ofcourse thats now what i need, i need it to be in order of "datetime" from the updates table, that is querried within the foreach loop. is there a way to after the loop and after the query to re-order the information? thanks Rabbit, gt
May 29 '12 #6
Rabbit
12,516 Recognized Expert Moderator MVP
Instead of storing everything in a string in one field, separate it out as one record for each. Then you can join on it.
May 29 '12 #7
George Thompson
33 New Member
is there a way to do that via php, or are you saying that each friend should have his/her own field in the mysql database? how would i go about doing that?

if your saying each friend should have their own fiel in the database that would take a lot of reconstruction of an already existing database..
May 29 '12 #8
George Thompson
33 New Member
ok so did a little research and found something that may work Rabbit. with the code above i am creating a multidimensiona l array correct? where the first array is the friends list, and the second array would be the information from the "updates" table? if thats really the case then i could sort by the second array, i found some code that might lead me in the right direction... here is the snippet i got from the other website.
http://www.informit.com/articles/art...31840&seqNum=6

Expand|Select|Wrap|Line Numbers
  1. function compare($x, $y)
  2. {
  3.  if ( $x[1] == $y[1] )
  4.   return 0;
  5.  else if ( $x[1] < $y[1] )
  6.   return -1;
  7.  else
  8.   return 1;
  9. }
  10.  
  11. $products = array( array( 'TIR', 'Tires', 100 ),
  12.           array( 'OIL', 'Oil', 10 ),
  13.           array( 'SPK', 'Spark Plugs', 4 ) );
  14.  
  15.  
  16. usort($products, 'compare');
  17.  
  18. print_r ($products);
  19.  
think that would work? with some modification of course. thanks
gt
May 29 '12 #9
Rabbit
12,516 Recognized Expert Moderator MVP
I don't know what you mean by they should each have their own files. A database is not a file management tool. But yes, I mean redesign the structure. Because as it is, it causes problems when trying to query data.
May 29 '12 #10

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

Similar topics

4
4139
by: jeff brubaker | last post by:
Hello, Currently we have a database, and it is our desire for it to be able to store millions of records. The data in the table can be divided up by client, and it stores nothing but about 7 integers. | table | | id | clientId | int1 | int2 | int 3 | ... | Right now, our benchmarks indicate a...
7
2090
by: Paolo | last post by:
I know I should not be doing this, but I find it very useful. I have a database in Access which stores data for a small company. Sometimes we need to add similar information to different tables. Currently I am already doing something similar by copying certain records into the same table. The only thing that changes is one field. Please...
17
2060
by: Wilfried | last post by:
Hi, I have 5 tables: main data1
1
2459
by: Moojjoo | last post by:
Good Monday Morning: I am at a dead stop and I am hoping some other developers out there can help me out. I have built a dataset that first contains a table populated from SQL Server then I am adding another table that is built from the same Query and data from active directory. I have built a parent/child relationship and see all the...
0
1854
by: Jerms | last post by:
Hello all, I've been using this site quite a bit since starting my project and have found it very helpful. I have run into a roadblock though that I cant seem to scrounge up a solution to. I have this dataset created (REACHpointsdataset) with a few tables in it (points, users, items, etc.), I also add another table to this database though...
23
3399
nehashri
by: nehashri | last post by:
hi i am designing a database using Ms Access and ASP. i have 3 tables in access namely 'PERSONAL', other as 'POLICY' and 3rd one is named as 'STAFF'. in the contact table i have ID, Name, Children as fields. Also in policy table the firlds are:- ID, date_of_policy, no_policy, amount_paid, amount_balance and similarly the 3rd ie., staff has few...
6
3504
by: BinaryGirl23 | last post by:
Hi Everyone : ) I hope that this question is actually something easy in disguise. Re: Access 2003 I have a report that displays information about which of our employees worked at a given job over a period of 2 weeks, and displays how much we paid them for their work at that job (using DSUM to calc all those records within that 2 week period)....
9
2248
zimes
by: zimes | last post by:
I am working with Microsoft Access 2003 from the Office XP pro suite. I have created several tables that have to do with Asset tracking along with purchasing. I have a table called Purchasing, Assets, Comapnay, AssetCategorty, and AssetNumber. The number I would like to create would take data from Purchasing and Assets, then create a number that...
5
1968
by: lawson | last post by:
In access 2003, how do I repeat the same data in different tables without typing it out again. That in another table with different name.
3
1487
by: kour | last post by:
i have got six tables and i have to loop the data from these tables into different table or excel sheet.
0
7923
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7852
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8216
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8349
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7974
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8221
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6629
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3845
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.