6 Replies - 538 Views - Last Post: 29 April 2018 - 08:50 AM Rate Topic: -----

#1 Foobarer   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 269
  • Joined: 28-March 17

Sorting two result sets by shared column

Posted 28 April 2018 - 02:30 PM

I fetch data from 2 different querys:

$stmt1->execute();
$stmt2->execute();


Now each query returns data from 2 different tables, with different numbers of columns, however both of them have a 'date' column, which is timestamp (example date on the table: 2018-04-28 20:31:23

Is it possible to merge the two result sets and order them by the date? Because I want to echo the rows ordered by the date

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Sorting two result sets by shared column

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14858
  • View blog
  • Posts: 59,283
  • Joined: 12-June 08

Re: Sorting two result sets by shared column

Posted 28 April 2018 - 02:35 PM

Typically you would use SQL UNION... But, no.. Disparate tables are difficult.. Maybe see about adding them to an array the size of the longest row.
Was This Post Helpful? 1
  • +
  • -

#3 Foobarer   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 269
  • Joined: 28-March 17

Re: Sorting two result sets by shared column

Posted 28 April 2018 - 02:49 PM

remember the SQL you helped me earlier? Then I will need to do it all again,
Because except for the 2 tables you helped me that we joined?, there are 2 more. And since you saw the other tables, how difficult will that be to "union" them? until today, the most advanced sql I did was SELECT!

**Edit: Should I create a new topic on SQL section?

This post has been edited by Foobarer: 28 April 2018 - 02:53 PM

Was This Post Helpful? 0
  • +
  • -

#4 Foobarer   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 269
  • Joined: 28-March 17

Re: Sorting two result sets by shared column

Posted 28 April 2018 - 03:17 PM

I have 2 SELECT statements from 2 different tables. Each returns different number of columns, however both of them has a 'date' column.

Is it possible to get a query that sort them by the date?
Was This Post Helpful? 0
  • +
  • -

#5 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14858
  • View blog
  • Posts: 59,283
  • Joined: 12-June 08

Re: Sorting two result sets by shared column

Posted 28 April 2018 - 04:57 PM

Well.. no. Like I mentioned if these are different tables with different columns/data then they shouldn't be sorted together... at best maybe jam it all into an array in the non-db side.. assuming the array column length matches the table with the longest column.. but even then that gets janky results.

You should step back and reevaluate why you need two disparate tables worth of data sorted together.
Was This Post Helpful? 1
  • +
  • -

#6 Foobarer   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 269
  • Joined: 28-March 17

Re: Sorting two result sets by shared column

Posted 29 April 2018 - 07:03 AM

modi thank you for the help. You're right - I feel like I am making too much issue of something that should be solved more easily, and instead I went on a bad approach.
I will tell you what I want to do: It's like WhatsApp: You have a list of last message from either "Groups" or "Friends". Now I store these messages in 2 separate tables: group_messages and personal_messages.
So I want to get, like in WhatsApp, the last message on any chat the user is participating, and display that last message sorted by date.

Perhaps you see there's a different approach?

This post has been edited by Foobarer: 29 April 2018 - 07:04 AM

Was This Post Helpful? 0
  • +
  • -

#7 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14858
  • View blog
  • Posts: 59,283
  • Joined: 12-June 08

Re: Sorting two result sets by shared column

Posted 29 April 2018 - 08:50 AM

Messages are just messages regardless of who they are from; person or group. I would rework the database relationship.

One message table.
One table that indicates a relationship between a message id and a user id.. depending on how you do 'groups' that could be a one to one with a 'groups table id', or you can make it a many to one relationship.. where one, or more, user ids map to one message id.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1