Jump to content

MySQL Join Returns 1 Null Row


unemployment

Recommended Posts

Can someone please tell me what syntax I am screwing up?

 

`users`.`id` AS `uid`,
`users`.`username` AS `username`,
`users`.`firstname` AS `firstname`,
`users`.`lastname` AS `lastname`,
`users`.`accounttype` AS `accounttype`,
`users`.`country` AS `country`,
`users`.`state` AS `state`,
`users`.`city` AS `city`,
`markers`.`lat`,
`markers`.`lng`,
`user_privacy`.`avatar` AS `privacy_avatar`,
`user_privacy`.`city` AS `privacy_city`,
`user_privacy`.`location` AS `privacy_location`,
SUM(`investor_info`.`capital_available`) AS `totalavailable`,
`capital`
FROM `users`
LEFT JOIN `investor_info`
ON `users`.`id` = `investor_info`.`uid`
INNER JOIN `assets_countries`
ON `users`.`country` = `assets_countries`.`country_id`
INNER JOIN `assets_states`
ON `users`.`state` = `assets_states`.`state_id`
LEFT JOIN `markers`
ON `users`.`id` = `markers`.`uid`
LEFT JOIN `user_privacy`
ON `users`.`id` = `user_privacy`.`uid`
LEFT OUTER JOIN employees
ON users.id = employees.userid
LEFT OUTER JOIN (
SELECT 
	companies.companyid, 
	SUM(companies.capital) AS `capital`
FROM  `companies`
GROUP BY companies.companytype) SumCompanies
ON employees.companyid = SumCompanies.companyid
WHERE (`assets_countries`.`country` LIKE '%{$country}%'
OR `assets_states`.`state` LIKE '%{$state}%'
OR `users`.`city` LIKE '%{$city}%'
) AND `users`.`status` > 2

Link to comment
Share on other sites

I want the left outer join the sums the capital to only sum capital for companies that I am employed by.  Right now it's summing capital for all companies is my companies table, NOT where my employee ID and company id are found in the employees table.

 

SELECT
`users`.`id`,
`users`.`firstname`,
`users`.`lastname`,
`users`.`username`,
`users`.`email`,
`users`.`gender`,
`users`.`accounttype`,
`users`.`personalweb`,
`users`.`guestviews`,
`users`.`iviews`,
`users`.`eviews`,
`users`.`credentials`,
`users`.`specialties`,
`users`.`country`,
`users`.`city`,
`users`.`state`,
`users`.`phonenumber`,
`users`.`dateofbirth` AS `dob`,
`users`.`mail_status`,
`users`.`status`,
`markers`.`lat`,
`markers`.`lng`,
`user_privacy`.`avatar` AS `privacy_avatar`,
`user_privacy`.`city` AS `privacy_city`,
`user_privacy`.`location` AS `privacy_location`,
`investor_info`.`investor_type`,
DATE_FORMAT(`users`.`dateofbirth`,'%D') AS `dayofbirth`,
DATE_FORMAT(`users`.`dateofbirth`,'%c') AS `monthofbirth`,
DATE_FORMAT(`users`.`dateofbirth`,'%Y') AS `yearofbirth`,
DATE_FORMAT(`users`.`dateofbirth`,'%D \of %M %Y') AS `dateofbirth`,
DATE_FORMAT(`users`.`signupdate`,'%h:%i %p %M %e, %Y') AS `signupdate`,
SUM(`investor_info`.`capital_available`) AS `totalavailable`,
`companytype`,
`capital`
FROM `users`
LEFT JOIN `investor_info`
ON `users`.`id` = `investor_info`.`uid`
LEFT JOIN `markers`
ON `users`.`id` = `markers`.`uid`
LEFT JOIN `user_privacy`
ON `users`.`id` = `user_privacy`.`uid`
LEFT OUTER JOIN employees
ON users.id = employees.userid
LEFT OUTER JOIN (
SELECT 
	companies.companyid, 
	companies.companytype AS `companytype`,
	SUM(companies.capital) AS `capital`
FROM  `companies`
GROUP BY companies.companytype) SumCompanies
ON employees.companyid = SumCompanies.companyid
WHERE `users`.`status` > 2 

Link to comment
Share on other sites

changing this line :

SUM(`investor_info`.`capital_available`) AS `totalavailable`,

To incorporate a CASE like this:

SUM(CASE WHEN SELECT `check_criteria`THEN SELECT `capital_field.value`ELSE 0 END) AS `totalavailable`,

Should get the result you're after.

 

Why would I need to check criteria?

 

In three tables there are...

 

Users

user_id

 

Employees

id user_id company_id

 

Companies

company_id

 

I just need to join those so that I only total the amount of capital where the user is employed by that company. 

 

Link to comment
Share on other sites

changing this line :

SUM(`investor_info`.`capital_available`) AS `totalavailable`,

To incorporate a CASE like this:

SUM(CASE WHEN SELECT `check_criteria`THEN SELECT `capital_field.value`ELSE 0 END) AS `totalavailable`,

Should get the result you're after.

 

Why would I need to check criteria?

 

In three tables there are...

 

Users

user_id

 

Employees

id user_id company_id

 

Companies

company_id

 

I just need to join those so that I only total the amount of capital where the user is employed by that company.

You need the check criteria to tell the SUM not to count every single result returned:

SUM(
CASE WHEN (
  SELECT employee.user_id 
  FROM employees 
    INNER JOIN users 
    ON employees.user_id = users.user_id 
    INNER JOIN companies 
    ON employees.company_id = companies.company_id 
  WHERE company.company_id = "VALUE" AND users.user_id = "VALUE") 
THEN (
  SELECT `capital_field.value`)
ELSE 
  0 
END) 
AS `totalavailable`

should work out the total value stored in the field "capital_field" which has both a matching company ID and user ID to whatever VALUE you choose to use.

Link to comment
Share on other sites

Here is the SQL.  I've made some modification though they still aren't quite right.  I really do appreciate the help, but I just wish I could get it working.  I wasn't sure how to adjust the VALUE in the case statement.  Any thoughts?

 

Here is my error: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SUM( CASE WHEN ( SELECT employees.userid FROM employees ' at line 46

 

         

SELECT
                `users`.`id`,
                `users`.`firstname`,
                `users`.`lastname`,
                `users`.`username`,
                `users`.`email`,
                `users`.`gender`,
                `users`.`accounttype`,
                `users`.`personalweb`,
                `users`.`guestviews`,
                `users`.`iviews`,
                `users`.`eviews`,
                `users`.`credentials`,
                `users`.`specialties`,
                `users`.`country`,
                `users`.`city`,
                `users`.`state`,
                `users`.`phonenumber`,
                `users`.`dateofbirth` AS `dob`,
			`users`.`mail_status`,
			`users`.`status`,
			`markers`.`lat`,
			`markers`.`lng`,
			`user_privacy`.`avatar` AS `privacy_avatar`,
			`user_privacy`.`city` AS `privacy_city`,
			`user_privacy`.`location` AS `privacy_location`,
                `investor_info`.`investor_type`,
                DATE_FORMAT(`users`.`dateofbirth`,'%D') AS `dayofbirth`,
                DATE_FORMAT(`users`.`dateofbirth`,'%c') AS `monthofbirth`,
                DATE_FORMAT(`users`.`dateofbirth`,'%Y') AS `yearofbirth`,
                DATE_FORMAT(`users`.`dateofbirth`,'%D \of %M %Y') AS `dateofbirth`,
                DATE_FORMAT(`users`.`signupdate`,'%h:%i %p %M %e, %Y') AS `signupdate`,
                SUM(`investor_info`.`capital_available`) AS `totalavailable`,
			`companytype`,
			`capital`
            FROM `users`
            LEFT JOIN `investor_info`
            ON `users`.`id` = `investor_info`.`uid`
		LEFT JOIN `markers`
		ON `users`.`id` = `markers`.`uid`
		LEFT JOIN `user_privacy`
		ON `users`.`id` = `user_privacy`.`uid`
            LEFT OUTER JOIN employees
            ON users.id = employees.userid
            LEFT OUTER JOIN (
			SUM(
			 CASE WHEN (
			  SELECT employees.userid 
			  FROM employees 
				INNER JOIN users 
				ON employees.userid = users.id 
				INNER JOIN companies 
				ON employees.companyid = companies.companyid 
			  WHERE company.companyid = employees.companyid AND users.id = employee.userid) 
			 THEN (
			  SELECT `company.capital`)
			 ELSE 
			  0 
			 END) 
			AS `totalavailable`) SumCompanies
            ON employees.companyid = SumCompanies.companyid
		WHERE `users`.`status` > 2

Link to comment
Share on other sites

What on earth is that bizarre case statement doing?

 

The goal of this is to only sum the column where the

 

user.id that is linked to employees.userid that is linked to employees.companyid that is linked to companies.companyid will then sum the companies.capital column.

 

It doesn't work though.  You'll see in the first example I was tried to get the sum of capital column.  The problem was that it was summing the entire capital column in the companies table and I only want it to sum the capital that is available to the specific employee / user.  The user can have multiple companies.

Link to comment
Share on other sites

I made some more edits and added in the SELECT, but I still get #1054 - Unknown column 'companies.capital' in 'field list'

 

I don't understand this because capital is a column in the companies table.  Does it need a FROM clause?

 

SELECT
    `users`.`id`,
    `users`.`firstname`,
    `users`.`lastname`,
    `users`.`username`,
    `users`.`email`,
    `users`.`gender`,
    `users`.`accounttype`,
    `users`.`personalweb`,
    `users`.`guestviews`,
    `users`.`iviews`,
    `users`.`eviews`,
    `users`.`credentials`,
    `users`.`specialties`,
    `users`.`country`,
    `users`.`city`,
    `users`.`state`,
    `users`.`phonenumber`,
    `users`.`dateofbirth` AS `dob`,
`users`.`mail_status`,
`users`.`status`,
`markers`.`lat`,
`markers`.`lng`,
`user_privacy`.`avatar` AS `privacy_avatar`,
`user_privacy`.`city` AS `privacy_city`,
`user_privacy`.`location` AS `privacy_location`,
    `investor_info`.`investor_type`,
    DATE_FORMAT(`users`.`dateofbirth`,'%D') AS `dayofbirth`,
    DATE_FORMAT(`users`.`dateofbirth`,'%c') AS `monthofbirth`,
    DATE_FORMAT(`users`.`dateofbirth`,'%Y') AS `yearofbirth`,
    DATE_FORMAT(`users`.`dateofbirth`,'%D \of %M %Y') AS `dateofbirth`,
    DATE_FORMAT(`users`.`signupdate`,'%h:%i %p %M %e, %Y') AS `signupdate`,
    SUM(`investor_info`.`capital_available`) AS `totalavailable`,
`companytype`,
`capital`
FROM `users`
LEFT JOIN `investor_info`
ON `users`.`id` = `investor_info`.`uid`
LEFT JOIN `markers`
ON `users`.`id` = `markers`.`uid`
LEFT JOIN `user_privacy`
ON `users`.`id` = `user_privacy`.`uid`
LEFT OUTER JOIN employees
ON users.id = employees.userid
LEFT OUTER JOIN (
   SELECT
SUM(
 CASE WHEN (
  SELECT employees.userid 
  FROM employees 
	INNER JOIN users 
	ON employees.userid = users.id 
	INNER JOIN companies 
	ON employees.companyid = companies.companyid 
  WHERE companies.companyid = employees.companyid AND users.id = employees.userid) 
 THEN (
  SELECT companies.capital)
 ELSE 
  0 
 END) 
AS `totalavailable`) SumCompanies
ON employees.companyid = SumCompanies.companyid
WHERE `users`.`status` > 2

Link to comment
Share on other sites

Yes, you will need to add companies to the FROM clause because the way it stands just now you arn't actualy refferencing it in the main SELECT.  You are using it in a sub query, and that subquery is providing a specific result set that the main SELECT refferences using the aliases that you have given it as though it was a table it's self.  the main Query does not get direct access to the tables used in the subquery as it is run independant of the main query.

Link to comment
Share on other sites

Yes, you will need to add companies to the FROM clause because the way it stands just now you arn't actualy refferencing it in the main SELECT.  You are using it in a sub query, and that subquery is providing a specific result set that the main SELECT refferences using the aliases that you have given it as though it was a table it's self.  the main Query does not get direct access to the tables used in the subquery as it is run independant of the main query.

 

Ok I sort of understand.  Now I'm getting an error that says  "Subquery returns more than 1 row".  Any ideas?

 

SELECT
    `users`.`id`,
    `users`.`firstname`,
    `users`.`lastname`,
    `users`.`username`,
    `users`.`email`,
    `users`.`gender`,
    `users`.`accounttype`,
    `users`.`personalweb`,
    `users`.`guestviews`,
    `users`.`iviews`,
    `users`.`eviews`,
    `users`.`credentials`,
    `users`.`specialties`,
    `users`.`country`,
    `users`.`city`,
    `users`.`state`,
    `users`.`phonenumber`,
    `users`.`dateofbirth` AS `dob`,
`users`.`mail_status`,
`users`.`status`,
`markers`.`lat`,
`markers`.`lng`,
`user_privacy`.`avatar` AS `privacy_avatar`,
`user_privacy`.`city` AS `privacy_city`,
`user_privacy`.`location` AS `privacy_location`,
    `investor_info`.`investor_type`,
    DATE_FORMAT(`users`.`dateofbirth`,'%D') AS `dayofbirth`,
    DATE_FORMAT(`users`.`dateofbirth`,'%c') AS `monthofbirth`,
    DATE_FORMAT(`users`.`dateofbirth`,'%Y') AS `yearofbirth`,
    DATE_FORMAT(`users`.`dateofbirth`,'%D \of %M %Y') AS `dateofbirth`,
    DATE_FORMAT(`users`.`signupdate`,'%h:%i %p %M %e, %Y') AS `signupdate`,
    SUM(`investor_info`.`capital_available`) AS `totalavailable`,
`companytype`,
`capital`
FROM `users`
LEFT JOIN `investor_info`
ON `users`.`id` = `investor_info`.`uid`
LEFT JOIN `markers`
ON `users`.`id` = `markers`.`uid`
LEFT JOIN `user_privacy`
ON `users`.`id` = `user_privacy`.`uid`
LEFT OUTER JOIN employees
ON users.id = employees.userid
LEFT OUTER JOIN (
   SELECT
SUM(
 CASE WHEN (
  SELECT employees.userid 
  FROM employees 
	INNER JOIN users 
	ON employees.userid = users.id 
	INNER JOIN companies 
	ON employees.companyid = companies.companyid 
  WHERE companies.companyid = employees.companyid AND users.id = employees.userid) 
 THEN (
  SELECT companies.capital
          FROM companies 
         )
 ELSE 
  0 
 END) 
AS `totalavailable`) SumCompanies
ON employees.companyid = SumCompanies.companyid
WHERE `users`.`status` > 2

Link to comment
Share on other sites

I fixed this query.  It's working now since I guess all I had to do was change the group by column, but I still want to add in another subquery.  Here is the  final version so far:

 

SELECT
`users`.`id`,
`users`.`firstname`,
`users`.`lastname`,
`users`.`username`,
`users`.`email`,
`users`.`gender`,
`users`.`accounttype`,
`users`.`personalweb`,
`users`.`guestviews`,
`users`.`iviews`,
`users`.`eviews`,
`users`.`credentials`,
`users`.`specialties`,
`users`.`country`,
`users`.`city`,
`users`.`state`,
`users`.`phonenumber`,
`users`.`dateofbirth` AS `dob`,
`users`.`mail_status`,
`users`.`status`,
`markers`.`lat`,
`markers`.`lng`,
`user_privacy`.`avatar` AS `privacy_avatar`,
`user_privacy`.`city` AS `privacy_city`,
`user_privacy`.`location` AS `privacy_location`,
`investor_info`.`investor_type`,
DATE_FORMAT(`users`.`dateofbirth`,'%D') AS `dayofbirth`,
DATE_FORMAT(`users`.`dateofbirth`,'%c') AS `monthofbirth`,
DATE_FORMAT(`users`.`dateofbirth`,'%Y') AS `yearofbirth`,
DATE_FORMAT(`users`.`dateofbirth`,'%D \of %M %Y') AS `dateofbirth`,
DATE_FORMAT(`users`.`signupdate`,'%h:%i %p %M %e, %Y') AS `signupdate`,
SUM(`investor_info`.`capital_available`) AS `totalavailable`,
`companytype`,
`capital`
FROM `users`
LEFT JOIN `investor_info`
ON `users`.`id` = `investor_info`.`uid`
LEFT JOIN `markers`
ON `users`.`id` = `markers`.`uid`
LEFT JOIN `user_privacy`
ON `users`.`id` = `user_privacy`.`uid`
LEFT OUTER JOIN employees
ON users.id = employees.userid
LEFT OUTER JOIN (
SELECT 
	companies.companyid, 
	companies.companytype AS `companytype`,
	SUM(companies.capital) AS `capital`
FROM  `companies`
GROUP BY companies.companyid) SumCompanies
ON employees.companyid = SumCompanies.companyidz
WHERE `users`.`status` > 2

 

I need to add in:

 

SELECT COUNT(`partner_id`) FROM `partners` WHERE (`user_id` = ${uid} OR `friend_id` = ${uid}) AND `approved` = 1

 

When I made this query I wrote it to work with the variables specified.  I tried adding this in as a subquery, but can't seem to get it to work.  I don't want the variables in the subquery. I  just want them to group for every user.

Link to comment
Share on other sites

Hi

 

Think this is the subquery you want::-

 

SELECT `user_id` AS PersonId, COUNT(`partner_id`) AS PartnerCount 
FROM `partners` 
WHERE `approved` = 1 
GROUP BY 
UNION
SELECT `friend_id` AS PersonId, COUNT(`partner_id`)  AS PartnerCount 
FROM `partners` 
WHERE `approved` = 1 
GROUP BY `friend_id`) AS SubQuery
GROUP BY PersonId

 

Trying to integrate that into your main query gives something like:-

 

SELECT
`users`.`id`,
`users`.`firstname`,
`users`.`lastname`,
`users`.`username`,
`users`.`email`,
`users`.`gender`,
`users`.`accounttype`,
`users`.`personalweb`,
`users`.`guestviews`,
`users`.`iviews`,
`users`.`eviews`,
`users`.`credentials`,
`users`.`specialties`,
`users`.`country`,
`users`.`city`,
`users`.`state`,
`users`.`phonenumber`,
`users`.`dateofbirth` AS `dob`,
`users`.`mail_status`,
`users`.`status`,
`markers`.`lat`,
`markers`.`lng`,
`user_privacy`.`avatar` AS `privacy_avatar`,
`user_privacy`.`city` AS `privacy_city`,
`user_privacy`.`location` AS `privacy_location`,
`investor_info`.`investor_type`,
DATE_FORMAT(`users`.`dateofbirth`,'%D') AS `dayofbirth`,
DATE_FORMAT(`users`.`dateofbirth`,'%c') AS `monthofbirth`,
DATE_FORMAT(`users`.`dateofbirth`,'%Y') AS `yearofbirth`,
DATE_FORMAT(`users`.`dateofbirth`,'%D \of %M %Y') AS `dateofbirth`,
DATE_FORMAT(`users`.`signupdate`,'%h:%i %p %M %e, %Y') AS `signupdate`,
SUM(`investor_info`.`capital_available`) AS `totalavailable`,
`companytype`,
`capital`,
SumPeople.PartnerCount
FROM `users`
LEFT JOIN `investor_info`
ON `users`.`id` = `investor_info`.`uid`
LEFT JOIN `markers`
ON `users`.`id` = `markers`.`uid`
LEFT JOIN `user_privacy`
ON `users`.`id` = `user_privacy`.`uid`
LEFT OUTER JOIN employees
ON users.id = employees.userid
LEFT OUTER JOIN (
SELECT 
	companies.companyid, 
	companies.companytype AS `companytype`,
	SUM(companies.capital) AS `capital`
FROM  `companies`
GROUP BY companies.companyid) SumCompanies
ON employees.companyid = SumCompanies.companyidz
LEFT OUTER JOIN (SELECT PersonId, SUM(PartnerCount) AS PartnerCount
FROM
(SELECT `user_id` AS PersonId, COUNT(`partner_id`) AS PartnerCount 
FROM `partners` 
WHERE `approved` = 1 
GROUP BY 
UNION
SELECT `friend_id` AS PersonId, COUNT(`partner_id`)  AS PartnerCount 
FROM `partners` 
WHERE `approved` = 1 
GROUP BY `friend_id`) AS SubQuery
GROUP BY PersonId) SumPeople
ON users.id = SumPeople.PersonId
WHERE `users`.`status` > 2 

 

All the best

 

Keith

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.