joining multiple tables in mysql code example

Example 1: mysql join two tables

SELECT user_id, user_name
FROM users
UNION
SELECT organization_id, organization_name
FROM organizations

Example 2: multiple joins mysql

SELECT dashboard_data.headline, dashboard_data.message, dashboard_messages.image_id, images.filename
FROM dashboard_data 
    INNER JOIN dashboard_messages 
        ON dashboard_message_id = dashboard_messages.id
    INNER JOIN images
        ON dashboard_messages.image_id = images.image_id

Example 3: how to combine 2 tables in MySQL

create table yourTableName
(
   select *from yourTableName1
)
UNION
(
   select *from yourTableName2
);

Example 4: mysql two joins

SELECT areas.name AS aname, COUNT(consultants.active) AS cct 
    FROM areas 
    LEFT JOIN consAreas 
      ON consAreas.area = areas.id 
    LEFT JOIN consultants 
      ON consultants.id = consAreas.cons
   WHERE areas.areaID = $area 
     AND consultants.active = 1
GROUP BY areas.name
ORDER BY areas.name

Tags:

Sql Example