Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

complex join query

Tags:

php

mysql

mysql5

I have MYSQL tables as follows

user TABLE {id INT}

profile TABLE {user_id INT, facebook_id varchar(50)}

messages TABLE {id INT, message TEXT, from_id INT, type enum('main','facebook'}

messages_to TABLE {user_id varchar(50), message_id INT}

profile.user_id REFERS To user.id
- the messages_to.message_id refers to the messages.ID column.
- the messages_to.user_id refers to profile.user_id IF messages.type = 'main' BUT
  if message_type = 'facebook' THEN messages_to.user_id REFERS to profile.facebook_id

I want to do a join query that basically selects all the messages to a specific person, but the thing is the messages_to.user_id can refer to either the person's facebook ID or the person's ID (a reference to user.id column).

So basically the query should work as follows

  • it should select all the messages in messages table, and if messages.type = 'facebook' checks if messages_to.user_id equals the person's FACEBOOK ID. (note that messages_to table stores the recipients for each messages ID)
  • BUT if the messages.type = 'main' checks if the messages_to.user_id equals the person's USER ID (USER.id)

Is it possible to do a mysql join query for that efficiently?

messages_tos table stores ALL the recipients for each message in the MESSAGES table. THERE CAN be MORE THAN ONE RECIPIENT for a message.

like image 725
Chris Hansen Avatar asked Aug 05 '11 16:08

Chris Hansen


People also ask

What is a complex join in SQL?

Overview. A complex join in SQL is also referred to as an outer join. It is not necessarily more complex than an inner join. It is referred to as "complex" simply because SQL is conducting an inner join in addition to gathering a little more information from one or more tables.

What is a complex query SQL?

Complex SQL is the use of SQL queries which go beyond the standard SQL of using the SELECT and WHERE commands. Complex SQL often involves using complex joins and sub-queries, where queries are nested in WHERE clauses. Complex queries frequently involve heavy use of AND and OR clauses.

What does complex query mean?

As the name suggests, a complex query has a complex syntax and can comprise multiple parts. A purpose of such a query is to search data based on several parameters. For instance, a complex query can include several joins across tables or have subqueries (a query nested within another query).

How do you write a complex SQL query?

Identify all the tables you'll need in the query. Join tables containing the data you need to display or the data used in the WHERE part of the query. Display all data to check if you've joined everything correctly and to see the result of such a query. Create all subqueries separately.


1 Answers

I guess this is the query.

SELECT messages.*,profile.* 
FROM messages 
JOIN messages_to ON messages.id = messages_to.message_id
JOIN profile ON 
  (profile.user_id = messages_to.user_id AND messages.type = 'main') 
  OR (profile.facebook_id = messages_to.user_id AND messages.type = 'facebook')  
like image 143
ace Avatar answered Sep 30 '22 09:09

ace