473,394 Members | 1,726 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,394 software developers and data experts.

ordering data by datetime from 2 different tables

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 2548
Rabbit
12,516 Expert Mod 8TB
Join the members table to the update table.
May 27 '12 #2
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
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_array" 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 Expert Mod 8TB
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
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 Expert Mod 8TB
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
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
ok so did a little research and found something that may work Rabbit. with the code above i am creating a multidimensional 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 Expert Mod 8TB
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
sorry meant to say "field" just forgot the "d"... did you get a chance to see the post i had made just after that? with the multidimensional array? what are your thoughts there? and for me to make each friend have their own field seems like it would be a lot of code? how do other "communities" structure their friend fields? thanks again buddy gt
May 29 '12 #11
Rabbit
12,516 Expert Mod 8TB
Whether or not it would work is moot if you can fix the SQL.

I wasn't saying to put a friend in their own fields. But to put a friend in their own record.

Expand|Select|Wrap|Line Numbers
  1. member friend
  2. bob    sally
  3. bob    billy
  4. sally  billy
May 29 '12 #12
hm, ok so your saying i should create a new table, call it "friends" or whatever, and in that table have that hold who is friends with whomever?
May 29 '12 #13
Rabbit
12,516 Expert Mod 8TB
Yes, that way you can join the members to the friend's updates and sort by the time all within SQL. You don't have to write code to compensate for the unnormalized data structure.
May 29 '12 #14
i see... sounds great. but here is the new problem with that idea... i already have a database set up with members and the members have friends... so i would have to go through one by one and re-set up their friends individually? might take me quite a while... any other suggestions? or maybe a quicker way to restructure the friends
May 29 '12 #15
Rabbit
12,516 Expert Mod 8TB
What's available to you to accomplish the restructuring will depend on the specific database system you're using. I suggest posting in that database's forum and link to this thread so they have some background. We have forums for the most used databases here so you should be covered.
May 29 '12 #16
ok thanks, im running mysql 5.1 but i think ive created a php script that will explode and insert into the new table. i really appreciate all of your help. ill start work on that tonight, i appreciate the help, better to restructure it now with it somewhat small system, as apposed to later where it might be to big of a job to handle. thanks again!!
May 29 '12 #17

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

Similar topics

4
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...
7
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....
17
by: Wilfried | last post by:
Hi, I have 5 tables: main data1
1
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...
0
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...
23
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...
6
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...
9
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,...
5
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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,...

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.