Error: The used SELECT statements have a different number of columns - sql

Why am I getting ERROR 1222 (21000): The used SELECT statements have a different number of columns from the following?
SELECT * FROM friends
LEFT JOIN users AS u1 ON users.uid = friends.fid1
LEFT JOIN users AS u2 ON users.uid = friends.fid2
WHERE (friends.fid1 = 1) AND (friends.fid2 > 1)
UNION SELECT fid2 FROM friends
WHERE (friends.fid2 = 1) AND (friends.fid1 < 1)
ORDER BY RAND()
LIMIT 6;
users:
+------------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------------+------+-----+---------+----------------+
| uid | int(11) | NO | PRI | NULL | auto_increment |
| first_name | varchar(50) | NO | | NULL | |
| last_name | varchar(50) | NO | | NULL | |
| email | varchar(128) | NO | UNI | NULL | |
| mid | varchar(40) | NO | | NULL | |
| active | enum('N','Y') | NO | | NULL | |
| password | varchar(64) | NO | | NULL | |
| sex | enum('M','F') | YES | | NULL | |
| created | datetime | YES | | NULL | |
| last_login | datetime | YES | | NULL | |
| pro | enum('N','Y') | NO | | NULL | |
+------------+---------------+------+-----+---------+----------------+
friends:
+---------------+--------------------------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------------------------------+------+-----+---------+----------------+
| friendship_id | int(11) | NO | MUL | NULL | auto_increment |
| fid1 | int(11) | NO | PRI | NULL | |
| fid2 | int(11) | NO | PRI | NULL | |
| status | enum('pending','accepted','ignored') | NO | | NULL | |
+---------------+--------------------------------------+------+-----+---------+----------------+

UNIONs (UNION and UNION ALL) require that all the queries being UNION'd have:
The same number of columns in the SELECT clause
The column data type has to match at each position
Your query has:
SELECT f.*, u1.*, u2.* ...
UNION
SELECT fid2 FROM friends
The easiest re-write I have is:
SELECT f.*, u.*
FROM FRIENDS AS f
JOIN USERS AS u ON u.uid = f.fid2
WHERE f.fid1 = 1
AND f.fid2 > 1
UNION
SELECT f.*, u.*
FROM FRIENDS AS f
JOIN USERS AS u ON u.uid = f.fid1
WHERE f.fid2 = 1
AND f.fid1 < 1
ORDER BY RAND()
LIMIT 6;
You've LEFT JOIN'd to the USERS table twice, but don't appear to be using the information.

Related

SQL count results on left join

I'm trying to get the total count of a table from a left join where there's a multiple of the same id. Here's my example below -
Table 1:
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| project_id | int(11) | NO | | NULL | |
| token | varchar(32) | NO | | NULL | |
| email | varchar(255) | NO | | NULL | |
| status | char(1) | NO | | 0 | |
| permissions | varchar(255) | YES | | NULL | |
| created | datetime | NO | | NULL | |
| modified | datetime | NO | | NULL | |
+-------------+--------------+------+-----+---------+----------------+
Table 2:
+------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(32) | NO | | NULL | |
| account_id | int(11) | NO | | NULL | |
| created | datetime | NO | | NULL | |
| modified | datetime | NO | | NULL | |
| active | tinyint(1) | YES | | 1 | |
+------------+-------------+------+-----+---------+----------------+
I have this statement so far -
SELECT account_id, (SELECT COUNT(invitations.id)
FROM invitations WHERE invitations.project_id = projects.id) AS inv_count
FROM projects order by account_id;
And here's a sample of the results:
+------------+-----------+
| account_id | inv_count |
+------------+-----------+
| 1 | 0 |
| 2 | 2 |
| 2 | 0 |
| 3 | 4 |
| 3 | 0 |
| 3 | 4 |
| 3 | 0 |
| 4 | 6 |
| 4 | 3 |
| 4 | 3 |
| 4 | 5 |
| 4 | 3 |
| 4 | 9 |
| 5 | 6 |
| 5 | 0 |
| 5 | 4 |
| 5 | 2 |
| 5 | 2 |
How do I get account_id to show once and the sum of inv_count to show as 1 line? So I should see -
+------------+-----------+
| account_id | inv_count |
+------------+-----------+
| 1 | 0 |
| 2 | 2 |
| 3 | 8 |
You only need to put your query in a derived table (and name it, say tmp) and then group by the account_id:
SELECT account_id,
SUM(inv_count) AS inv_count
FROM
( SELECT account_id,
(SELECT COUNT(invitations.id)
FROM invitations
WHERE invitations.project_id = projects.id
) AS inv_count
FROM projects
) AS tmp
GROUP BY account_id
ORDER BY account_id ;
To simplify it farther, you can convert the inline subquery to a LEFT join. This way, no derived table is needed. I've also added aliases and removed the ORDER BY. MySQL does an implicit ORDER BY when you have GROUP BY so it's not needed here (unless you want to order by some other expression, different from the one you group by):
SELECT
p.account_id,
COUNT(i.id) AS inv_count
FROM
projects AS p
LEFT JOIN
invitations AS i
ON i.project_id = p.id
GROUP BY
p.account_id ;

Return null on condition in sub-table

Say i have 2 tables, person and job.
+--------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | NULL | |
| job_id | int(11) | NO | | NULL | |
+--------+--------------+------+-----+---------+----------------+
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| j_id | int(11) | NO | PRI | NULL | auto_increment |
| j_name | varchar(255) | NO | | NULL | |
| j_active | tinyint(1) | NO | | 0 | |
+----------+--------------+------+-----+---------+----------------+
How would i do a select where it only returns a job_id where j_active = 1 and otherwise return 0 or NULL? So, I would want to always return all persons but when their job isn't active i dont want to return their job id
select * from person p left join job j on p.job_id=j.j_id and j.j_active=1
A case statement should work. Something like:
select name, case when j_active=1 then job_id else null end as job_id
from person join job on (person.job_id=job.j_id)

mysql three joins

I have a problem with mysql
I have 3 tables:
Deposit
+-------------------+-------------+------+-----+
| Field | Type | Null | Key |
+-------------------+-------------+------+-----+
| id | bigint(20) | NO | PRI |
| status | int(2) | NO | |
| depositDate | datetime | NO | MUL |
| reversePayment_id | bigint(20) | YES | UNI |
| claim_id | int(2) | NO | UNI |
| payment_id | bigint(20) | YES | UNI |
+-------------------+-------------+------+-----+
Payment
+--------------------------+---------------+------+-----+
| Field | Type | Null | Key |
+--------------------------+---------------+------+-----+
| id | int(10) | NO | PRI |
| paymentDate | timestamp | NO | MUL |
| pin | int(10) | NO | MUL |
| balanceChange | decimal(15,2) | YES | |
Claim
+------------------------+--------------+------+-----+
| Field | Type | Null | Key |
+------------------------+--------------+------+-----+
| id | int(11) | NO | PRI |
| fullName | varchar(100) | NO | |
| depositSum | blob | NO | |
| ip | varchar(39) | NO | |
| status | int(2) | NO | |
+------------------------+--------------+------+-----+
I try to select deposits (with claims) payment or reversePayment were between two dates, I perform this query with 3 joins:
EXPLAIN SELECT this_.id AS id60_3_, ..., fcpayment2_.id AS id59_0_, ..., reversepay3_.id AS id59_1_, ..., cl1_.id AS id61_2_, ...
FROM Deposit this_
INNER JOIN Payment fcpayment2_ ON this_.payment_id = fcpayment2_.id
LEFT OUTER JOIN Payment reversepay3_ ON this_.reversePayment_id = reversepay3_.id
INNER JOIN Claim cl1_ ON this_.claim_id = cl1_.id
WHERE (
(
fcpayment2_.paymentDate >= '2010-08-04 21:00:00'
AND fcpayment2_.paymentDate <= '2010-08-05 08:01:00'
)
OR (
reversepay3_.paymentDate >= '2010-08-04 21:00:00'
AND reversepay3_.paymentDate <= '2010-08-05 08:01:00'
)
)
ORDER BY this_.depositDate DESC
the result is
+----+-------------+--------------+--------+--------------------------------------------------------------------+----------+---------+-----------------------------------------+--------+---------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------------+--------+--------------------------------------------------------------------+----------+---------+-----------------------------------------+--------+---------------------------------+
| 1 | SIMPLE | cl1_ | ALL | PRIMARY | NULL | NULL | NULL | 426588 | Using temporary; Using filesort |
| 1 | SIMPLE | this_ | eq_ref | claim_id,payment_id,FKDB5A0548511B6CDD,FKDB5A054867BA4108 | claim_id | 4 | portal.cl1_.id | 1 | |
| 1 | SIMPLE | fcpayment2_ | eq_ref | PRIMARY,paymentDate,date | PRIMARY | 4 | portal.this_.payment_id | 1 | Using where |
| 1 | SIMPLE | reversepay3_ | eq_ref | PRIMARY | PRIMARY | 4 | portal.this_.reversePayment_id | 1 | Using where |
+----+-------------+--------------+--------+--------------------------------------------------------------------+----------+---------+-----------------------------------------+--------+---------------------------------+
Why the first table in result is cl1_ and why mysql doesn't use key?
Because you used the keyword 'Explain', and because cl1_ is the alias you gave the table in your query.
I don't understand your question about the key.

MYSQL select statement conditions

query:
SELECT u.deviceID, u.userName, u.contactNo, u.rating
FROM User u
INNER JOIN TaxiQuery t ON u.deviceID = t.seat1
OR u.deviceID = t.seat2
OR u.deviceID = t.seat3
OR u.deviceID = t.seat4
WHERE t.queryID = 3;
+--------------------------------------+----------+-----------+--------+
| deviceID | userName | contactNo | rating |
+--------------------------------------+----------+-----------+--------+
| 00000000-0000-1000-8000-0016CB8B3C8E | uuuuuu | 55555 | 5 |
+--------------------------------------+----------+-----------+--------+
describe user;
+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| deviceID | varchar(100) | NO | PRI | NULL | |
| userName | varchar(100) | YES | | NULL | |
| contactNo | int(11) | YES | | NULL | |
| emailAddr | varchar(100) | YES | | NULL | |
| rating | int(11) | YES | | NULL | |
+-----------+--------------+------+-----+---------+-------+
mysql> describe taxiQuery;
+--------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+----------------+
| queryID | int(11) | NO | PRI | NULL | auto_increment |
| destination | varchar(100) | YES | | NULL | |
| deptTime | varchar(100) | YES | | NULL | |
| startingPt | varchar(100) | YES | | NULL | |
| boardingPass | varchar(100) | YES | | NULL | |
| miscInfo | varchar(100) | YES | | NULL | |
| seat1 | varchar(100) | YES | | NULL | |
| seat2 | varchar(100) | YES | | NULL | |
| seat3 | varchar(100) | YES | | NULL | |
| seat4 | varchar(100) | YES | | NULL | |
+--------------+--------------+------+-----+---------+----------------+
What i want is to display the user's information if they exist in (seat1/seat2/seat3/seat4) in TaxiQuery. But i am only able to output one result when they are suppose to be three.
May i know how do i modify mysql statement to display the user's information when (seat1-4 is the foreign key to the deviceID of User's table) when seat1, seat2, seat3, seat4 contains the deviceID of the users?
As far as I can tell, it should work if you don't do an INNER join. I think the INNER keyword is telling mySQL to only include each source a maximum of once, so it will only use one copy of the TaxiQuery, when you actually need up to four (one per seat).

MySQL: Removing duplicate columns on Left Join, 3 tables

I have a table that uses 3 foreign keys into other tables. When I perform a left join, I get duplicate columns. MySQL says that the USING syntax will reduce the duplicate columns, but there aren't examples for multiple keys.
Given:
mysql> describe recipes;
+------------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------+------------------+------+-----+---------+-------+
| ID_Recipe | int(11) | NO | PRI | NULL | |
| Recipe_Title | char(64) | NO | | NULL | |
| Difficulty | int(10) unsigned | NO | | NULL | |
| Elegance | int(10) unsigned | NO | | NULL | |
| Quality | int(10) unsigned | NO | | NULL | |
| Kitchen_Hours | int(10) unsigned | NO | | NULL | |
| Kitchen_Minutes | int(10) unsigned | NO | | NULL | |
| Total_Hours | int(10) unsigned | NO | | NULL | |
| Total_Minutes | int(10) unsigned | NO | | NULL | |
| Serving_Quantity | int(10) unsigned | NO | | NULL | |
| Description | varchar(128) | NO | | NULL | |
| ID_Prep_Text | int(11) | YES | | NULL | |
| ID_Picture | int(11) | YES | | NULL | |
| Category | int(10) unsigned | NO | | NULL | |
| ID_Reference | int(11) | YES | | NULL | |
+------------------+------------------+------+-----+---------+-------+
15 rows in set (0.06 sec)
mysql> describe recipe_prep_texts;
+------------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------+---------------+------+-----+---------+-------+
| ID_Prep_Text | int(11) | NO | PRI | NULL | |
| Preparation_Text | varchar(2048) | NO | | NULL | |
+------------------+---------------+------+-----+---------+-------+
2 rows in set (0.02 sec)
mysql> describe recipe_prep_texts;
+------------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------+---------------+------+-----+---------+-------+
| ID_Prep_Text | int(11) | NO | PRI | NULL | |
| Preparation_Text | varchar(2048) | NO | | NULL | |
+------------------+---------------+------+-----+---------+-------+
2 rows in set (0.02 sec)
mysql> describe mp_references;
+--------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+---------+------+-----+---------+-------+
| ID_Reference | int(11) | NO | PRI | NULL | |
| ID_Title | int(11) | YES | | NULL | |
| ID_Category | int(11) | YES | | NULL | |
+--------------+---------+------+-----+---------+-------+
3 rows in set (0.00 sec)
My query statement:
SELECT *
FROM Recipes
LEFT JOIN (Recipe_Prep_Texts, Recipe_Pictures, mp_References)
ON (
Recipe_Prep_Texts.ID_Prep_Text = Recipes.ID_Prep_Text AND
Recipe_Pictures.ID_Picture = Recipes.ID_Picture AND
mp_References.ID_Reference = Recipes.ID_Reference
);
My objective is to get one row of all the columns from the join without duplicate columns. I'm using MySQL C++ Connector to send the SQL statements and retrieve result sets. I believe that the C++ Connector is having issues with duplicate column names.
So what is the SQL statement syntax that I should use?
Reference to MySQL JOIN syntax
I believe the following should work:
SELECT *
FROM Recipes
LEFT JOIN Recipe_Prep_Texts USING (ID_Prep_Text)
LEFT JOIN Recipe_Pictures USING (ID_Picture)
LEFT JOIN mp_References USING (ID_Reference)
Since it looks like most of the tables you are joining on have a few columns except for the first one, how about:
SELECT Recipes.*,
Recipe_Prep_Texts.Preparation_Text,
Recipe_Pictures.Foo, -- describe is missing in OP
mp_References.ID_Title,
mp_References.ID_Category
FROM Recipes
LEFT JOIN (Recipe_Prep_Texts, Recipe_Pictures, mp_References)
ON (
Recipe_Prep_Texts.ID_Prep_Text = Recipes.ID_Prep_Text AND
Recipe_Pictures.ID_Picture = Recipes.ID_Picture AND
mp_References.ID_Reference = Recipes.ID_Reference
);
I can't tell you how many times I wished I had
SELECT (* - foo) FROM table
especially in cases where foo is some huge field like a BLOB and I just want to see everything else without breaking the formatting.
You are selecting * from the combined resulting table. Limit that * to whatever columns you want to keep.
Try the following query:
SELECT name,ac,relation_name
FROM table1
LEFT JOIN table2 USING (ID_Prep_Text)
LEFT JOIN table3 USING (ID_Picture);