Currently have a function which uses the following SQL statement:
SELECT `plans`.`concurrents`
FROM `plans`
LEFT JOIN `users` ON `users`.`membership` = `plans`.`ID`
WHERE `users`.`ID` = ?`
I am trying to add into the SQL statement so it also adds on the value within the 'extra_concurrents' column in the 'users' table to the value which is returned from the SQL statement above.
I tried but have no luck:
SELECT `plans`.`concurrents`
FROM `plans`
LEFT JOIN `users` ON `users`.`membership` AND `users`.`extra_concurrents` = `plans`.`ID`
WHERE `users`.`ID` = ?`
I need to return the total value of plans.concurrents and users.extra_concurrents added together
I need to say something about your query. The left join is utterly unnecessary. Your where clause is turning it into an inner join.
Then, I recommend table aliases, to simplify the query. And the rest is addition:
SELECT p.concurrents + coalesce(u.extra_concurrents, 0)
FROM plans p JOIN
users u
ON u.membership = p.ID
WHERE u.ID = ?;
Also notice the coalesce(), just in case the users.extra_concurrents column is NULL.
When you join the tables you can call the columns from the other table
SELECT `plans`.`concurrents`,`users`.`extra_concurrents`
FROM `plans`
LEFT JOIN `users` ON `users`.`membership` = `plans`.`ID`
WHERE `users`.`ID` = ?`
You'll need to use multiple joins instead of an "and". Without seeing your data structure it's hard to say exactly what you'll need, but probably something like this:
SELECT `plans`.`concurrents`
FROM `plans` `A`
LEFT JOIN `users` `B` ON `B`.`membership` = `A`.`ID`
INNER JOIN `users` `C` ON `C`.`extra_concurrents` = `A`.`ID`
WHERE `users`.`ID` = ?`
SELECT sum((`plans`.`concurrents`)+(`users`.`extra_concurrents`))
FROM `plans`
LEFT JOIN `users` ON `users`.`membership` = `plans`.`ID`
WHERE `users`.`ID` = ?`
Note : its not good to add where condition on Left table.
Good Luck
Related
i have three tables and want to run INNER JOIN and IN clause on them.
can anyone tell me where i am doing wrong
SELECT `tblinvoices`.id,`tblinvoices`.userid,`firstname`,`lastname`
FROM `tblinvoices`
WHERE `paymentmethod`IN
(SELECT `gateway` FROM `tblpaymentgateways` WHERE `setting`='type' AND `value` = 'CC')
INNER JOIN `tblclients` ON `tblinvoices`.userid=`tblclients`.id"
JOIN comes before WHERE:
SELECT tblinvoices.id,
tblinvoices.userid,
firstname,
lastname
FROM
tblinvoices
INNER JOIN tblclients
ON tblinvoices.userid = tblclients.id
WHERE
paymentmethod IN
(select gateway
FROM tblpaymentgateways
WHERE setting='type'
AND value = 'CC')
I struggle to use join on multiple tables. When I try to do this:
SELECT `absences`.*, `employee`.*, `type`.*
FROM `absences`, `type`
LEFT JOIN `login`.`employee` ON `absences`.`employee_FK` = `employee`.`employee_ID`
I get this:
Unknown column 'absences.employee_FK' in 'on clause'
'absences.employee_FK' exists in my DB.
I want to display the user data and the type of the absence. How can I do that? I dont understand joins too well yet.
Looks like your just trying to join two tables, because you don't have a join condition for the type table in your query:
SELECT *
FROM absences
LEFT JOIN employee ON absences.employee_FK = employee.employee_ID
If you want to join to the type table too:
SELECT *
FROM absences
LEFT JOIN type ON absences.type_FK = type.type_ID
LEFT JOIN employee ON absences.employee_FK = employee.employee_ID
You have to select all the tables for using the JOIN condition.
The example goes like this:
SELECT `employee.*`, `absences.*`, `type.*`
FROM `employee`
JOIN `absences`
ON `employee`.`employee_ID` = `absences`.`employee_FK`
JOIN `type`
ON `absences`.`type_FK` = `type`.`type_ID`
JOIN `on_off`
ON `on_off`.`on_off_ID` = `employee`.`on_off_FK`;
You can modify the query as per your requirement.
You can work on the script below. Add Where clause at the end if necessary. Not tested...
SELECT * from absences a
inner join type t on (t.typeID = a.type_FK)
inner join employee e on (e.employee_ID = a.employee_FK)
This might be what you are looking for
select * from `absences` a
left outer join `employee` e on e.`employee_ID` on a.`employee_FK`
left outer join `type` t on t.`type_ID`=a.`type_FK`
left outer join `on_off` o on o.`on_off_ID`=e.`on_off_FK`
You have to use join for all tables:
SELECT `absences`.*, `employee`.*, `type`.*
FROM `absences`
JOIN `type` on `absences`.`type_fk` = `type`.`type_ID`
LEFT JOIN `login`.`employee` ON `absences`.`employee_FK` = `employee`.`employee_ID`
Here is an example query:
SELECT tags.serial_number, tags.expiry_date, tags.activation_date, users.email, users.offers, users.newsletter, pets.name as petname
FROM `tags`
JOIN `users` on users.id = tags.user_id
JOIN `pets` on pets.id = tags.pet_id
WHERE users.username = 'myusername' AND users.email = 'itsme#mydomain.com'
In this scenario, the only results that will be returned are those tags that are assigned to both a user and a pet. But what I want is to be able to receive all tags, regardless of whether or not they are assigned to a pet.
If there some kind of "IF there is a pet, show the petname, ELSE just return the pet name as NULL" or something like that?
Try using a Left Outer Join Which will populate rows that don't join to the table with Nulls
SELECT tags.serial_number, tags.expiry_date, tags.activation_date, users.email, users.offers, users.newsletter, pets.name as petname
FROM `tags`
JOIN `users` on users.id = tags.user_id
Left Outer JOIN `pets` on pets.id = tags.pet_id
WHERE users.username = 'myusername' AND users.email = 'itsme#mydomain.com'
The quick answer is to use LEFT JOIN in place of JOIN.
(I'm sure this question has asked on StackOverflow before ...)
I have two tables. I want to draw a sample of the first table except where the person in the first table is also in a second table. Am having trouble doing this seemingly simple query.
table users
id|name
table catuser
id|userid|catid
I have tried
SELECT u.*,c.userid FROM `users` u
LEFT JOIN `catuser` c
ON (u.id = c.userid AND c.userid <> '197')
WHERE u.id = '1'
and variations to no avail. Would appreciate any suggestions.
How abt. this:
SELECT u.*,c.userid
FROM `users` u
LEFT JOIN `catuser` c
ON u.id = c.userid
WHERE u.id = '1'
AND c.userid <> '197'
AND c.userid is null
SELECT * FROM users WHERE id NOT IN (SELECT DISTINCT userid FROM catuser)
If you want to query only users that have one or more categories, you can use a WHERE EXISTS query:
SELECT u.* FROM `users` u
WHERE EXISTS (SELECT * FROM catuser WHERE catuser.userid = u.id)
Another possibility is to do a left join, and check whether the join succeeded on checking on null:
SELECT u.*, c.* FROM `users` u
LEFT JOIN catuser c ON u.id = c.userid
WHERE c.id IS NOT NULL
If there is no corresponding row in catuser, all catuser fields will be null. By checking whether c.id is not null, you only include the rows with a category.
Note that the join may return a user multiple time, if he is in multiple categories.
Is there a way to avoid adding a second LEFT JOIN for the table "social_mcouple" to query where social_members.m_id = social_mcouple.c_id below?
$res = #mysql_query("SELECT *, DATE_FORMAT(m_lld,'%m/%d/%y') AS m_lld_formatted FROM social_members
LEFT JOIN social_member_types ON t_id=m_type WHERE m_user='".$en['user']."'");
If there will always be a social_mcouple that corresponds to social_members, or you're only interested in rows where there is a correspondence then you may use an INNER JOIN. If you need all social_members regardless of whether there is a corresponding social_mcouple then you will need a LEFT JOIN. The LEFT JOIN will give you all rows with social_mcouple.* set to NULL where there is not a match.
The performance hit will really depend on the size of your datasets.
EDIT: adding a sample UNION query.
$res = #mysql_query("
(SELECT social_members.*, social_member_types.*, DATE_FORMAT(m_lld,'%m/%d/%y') AS m_lld_formatted,
NULL AS mcouple1, NULL AS mcouple2, NULL AS mcouple3
FROM social_members
LEFT JOIN social_member_types ON t_id=m_type
WHERE m_user='".$en['user']."' AND m_type != 2)
UNION
(SELECT social_members.*, social_member_types.*, DATE_FORMAT(m_lld,'%m/%d/%y') AS m_lld_formatted,
social_mcouple.mcouple1, social_mcouple.mcouple2, social_mcouple.mcouple3
FROM social_members
LEFT JOIN social_member_types ON t_id=m_type
JOIN social_mcouple ON social_members.m_id = social_mcouple.c_id
WHERE m_user='".$en['user']."' AND m_type = 2)
");