My database structure is the following:
Table RESERVATION
ID
responsible_person
number_of_persons
Table SIZE
ID
size
Many-to-many Table RESERVATIONS_HAS_SIZE
RESERVATION_ID
SIZE_ID
A person can register for example 3 persons. You have to choose the sizes of these 3 peoples (S, M or L).
My query looks like this:
SELECT * FROM RESERVATION
INNER JOIN RESERVATIONS_HAS_SIZE
ON RESERVATION.ID = RESERVATIONS_HAS_SIZE.RESERVATION_ID
INNER JOIN SIZES
ON RESERVATIONS_HAS_SIZE.SIZE_ID = SIZE.ID
The problem is now I'm getting 3 records back. I only want one record back and for example one field with sizes: S, S, L.
Is this possible? And if so, how can I do this?
You can use GROUP_CONCAT():
SELECT GROUP_CONCAT(s.Size SEPARATOR ', ')
FROM RESERVATION r INNER JOIN
RESERVATIONS_HAS_SIZE rhs
ON r.ID = rhs.RESERVATION_ID INNER JOIN
SIZES s
ON rhs.SIZE_ID = s.ID;
Related
The main table has 4 columns:
User Activity Table
userActivityId userId therapistId activityId
1 1 1 1
Each of these columns is a table and these values are all foreign keys.
Basically im trying to run a query that will join to the users table and pull their first and last name based off the user Id.Same thing with therapist - join to the therapist table, pull first + last name.And finally Join to the Activity table and pull the activity name and path from the activity Id
The other tables look like this:
User Table
userId fName lName
Therapist Table
therapistId therapistFirstName therapistLastName
Activity Table
activityId activityTitle activityPath
So far my query looks like
SELECT
User_Activities.userId,
User_Activities.therapistId,
User_Activities.activityId,
Activities.activityTitle,
Activities.activityPath,
Users.fName,
users.lName,
Therapists.therapistFirstName,
Therapists.therapistLastName
FROM
User_Activities
INNER JOIN Users
ON User_Activities.userId = Users.userId
INNER JOIN Therapists ON
User_Activities.therapistId = Therapists.therapistId
INNER JOIN Activities ON
Activities.activityId = User_Activities.userActivityId
WHERE
User_Activities.userId = 1;
When I run this query It only returns 1 row as a result. However there are two activities in the User_Activites table assigned to userId 1.
If I change : INNER JOIN Activities ON
Activities.activityId = User_Activities.userActivityId
from an INNER JOIN to the LEFT JOIN it will display the second row, however the activityTitle and activityPath will be displayed as NULL in the second row.
userActivityId userId therapistId activityId activityId activityTitle activityPath fName lName therapistFirstName therapistLastName
1 1 1 1 1 Brain GZZ0zpUQ S C M D
11 1 1 1 NULL NULL NULL S C M D
You have pretty much answered your question. The second activity does not have a valid ActivityId.
If you want all activities for a user, then you should phrase the query as:
SELECT . . .
FROM Users u LEFT JOIN
User_Activities ua
ON ua.userId = u.userId LEFT JOIN
Therapists t
ON ua.therapistId = t.therapistId LEFT JOIN
Activities a
ON a.activityId = ua.userActivityId
WHERE u.userId = 1;
You want to start with the table where you want to keep all the rows. Then use LEFT JOIN to bring in other tables.
Two other changes of note:
Table aliases are used to simplify reading and writing the query. The SELECT needs to change to use the aliases.
The WHERE clause refers to the Users table rather than UserActivities.
This is a vote system, where candidate can be voted from different(limited) places. and I want the number of vote per place of each candidate.
I have 3 tables
TABLE candidate
------------------
id
name
TABLE place
------------------
id
label
TABLE vote
------------------
id
id_candidate
id_vote
no_votes // represents the amount of votes in this place for that particular candidate
Suppose I have 10 candidates and 15 different places, I'm trying to make a query that will return 10*15 = 150 rows even if there is no votes, keeping NULL value for ids that are not present in the relational table(which i can replace by 0).
But i'm not making the correct query
Here is the query i made so far (i've tried many modification, inner, outer joins... but nothing worked)
SELECT *
FROM votes
RIGHT JOIN candidate ON candidate.id = candidate_id
LEFT JOIN palce ON place.id = place_id
First, if you want the number of votes per candidate, then you should be thinking "aggregation".
Second, don't mix left and right joins in a query. It is just confusing. Start with the table where you want to keep all the rows, and then just use left join.
So, something like this:
SELECT c.*,
SUM(p.place_name = 'place1') as place1,
SUM(p.place_name = 'place2') as place2,
SUM(p.place_name = 'place3') as place3
FROM candidate c LEFT JOIN
votes v
ON c.id = v.candidate_id LEFT JOIN
place p
ON p.id = v.place_id
GROUP BY c.id;
Considering:
TABLE vote
------------------
id PK
id_candidate FK to candidate
id_vote FK to place
no_votes
-
SELECT CA.name,
PL.label,
SUM(VO.no_votes) as votes
FROM candidate CA
LEFT JOIN vote VO ON CA.id = VO.id_candidate
LEFT JOIN place PL ON PL.id = VO.id_vote
GROUP BY CA.id, PL.id
I am trying to optimise my php by doing as much work on the MySQL server as possible. I have this sql query which is pulling data out of a leads table, but at the same time joining two tags tables to combine the result. I am looking to add a company which is linked through a relations table.
So the table that holds the relationship between the two is relations_value which simply states (I add example data)
parenttable (companies) | parentrecordid (10) | childtable (leads) | childrecordid (1)
the companies table has quite a few columns but the only two relevant are;
id (10) | companyname (my company name)
So this query currently grabs everything I need but I want to bring the companyname into the query:
SELECT leads.id,
GROUP_CONCAT(c.tag ORDER BY c.tag) AS tags,
leads.status,
leads.probability
FROM `gs_db_1002`.leads
LEFT JOIN ( SELECT *
FROM tags_module
WHERE tagid IN ( SELECT id
FROM tags
WHERE moduleid = 'leads' ) ) as b
ON leads.id = b.recordid
LEFT JOIN `gs_db_1002`.tags as c
ON b.tagid = c.id
GROUP BY leads.id,
leads.status,
leads.probability
I need to be able to go into the relations_values table and pull parenttable and parentrecordid by selecting childtable = leads and childrecordid = 1 and somehow join these so that I am able to get companyname as a column in the above query...
Is this possible?
I have created a sqlfiddle: sqlfiddle.com/#!2/023fa/2 So I am looking to add companies.companyname as column to the query.
I don't know what your primary keys and foreign keys are that link each table together.. if you could give a better understanding of what ID's are linked to eachother it would make this a lot easier... however i did something that does return the correct result... but since all of the ID's are = 1 then it could be incorrect.
SELECT
leads.id, GROUP_CONCAT(c.tag ORDER BY c.tag) AS tags,
leads.status, leads.probability, companyname
FROM leads
LEFT JOIN (
SELECT * FROM tags_module WHERE tagid IN (
SELECT id FROM tags WHERE moduleid = 'leads' )
) as b ON leads.id = b.recordid
LEFT JOIN tags as c ON b.tagid = c.id
LEFT JOIN relations_values rv on rv.id = b.recordid
LEFT JOIN companies c1 on c1.createdby = rv.parentrecordid
GROUP BY leads.id,leads.status, leads.probability
I suppose this is a very common question and possibly is duplicated, but I didn't know how to search it. This is my scenario: I have 3 tables: registers, foods and recipes. Tables structure simplified:
Registers
id | item_id | type (0 food|1 recipe)
(item_id can be whatever a food id or recipe id)
Foods
id | name | weight
Recipes
id | name | num_ingredients
What I want is to query the registers table and return the food or recipe name (and some specific fields from each table, like weight in foods and num_ingredients in recipes) based on the type field being 0 or 1.
If the tables structure is not the best, I would thank to help me find the best solution.
Thanks a lot in advance.
You need to combine the two detail tables with a UNION, and then JOIN them with the Registers table using the ID and type fields.
SELECT item_id, name, data
FROM Registers r
JOIN (SELECT 0 type, id, name, weight as data
FROM Foods
UNION
SELECT 1 type, id, name, num_ingredients as data) x
ON r.item_id = x.id AND r.type = x.type
GolezTroi's answer where he does the UNION after JOIN is probably better, though, since it doesn't create a large temporary table before joining.
You can do this like so:
select r.id, r.type, f.name
from
Registers r
inner join Foods f on f.id = r.item_id and r.type = 0
union all
select r.id, r.type, c.name
from
Registers r
inner join Recipes c on c.id = r.item_id and r.type = 1
But your table structure isn't ideal. First of all, since r.item_id can contain an id from two tables, it is impossible to add a constraint that enforces referential integrity. You'll need a trigger to check it, which is more complex and slower.
Instead, I'd choose to make the relation the other way around:
Add a register_id to Foods and Recipes. Then, you can write your query like this:
select r.id, r.type, f.name
from
Registers r
inner join Foods f on f.register_id = r.id
union all
select r.id, r.type, c.name
from
Registers r
inner join Recipes c on c.register_id = r.id
That's almost the same, but you don't need the type and it allows you to make proper foreign key constraints.
I have 2 tables
1_products
id, code, make, model, fk_group_id
1_stock
id, stock, repair
I want to be able to return all of the rows in both tables based on the match in the first. Say WHERE fk_group_id = 11
Here is one:
SELECT *
FROM products AS p
INNER JOIN stock AS s ON p.id = s.id
WHERE fk_group_id = 11