sql union error - mysql

I always get the "the used select statements have different number of columns" error.
select Sublessee_uname, Sublessee_fname, Sublessee_mname, Sublessee_fname from sublessee
union
select Sublessee_space, Sublessee_product from space_reserve

The error is pretty much self-explanatory. In the first SELECT you have 4 columns, whilst in the second you have 2 columns. In order to use UNION, the number of columns in both SELECTs must match.

In Union Case no of columns should be same in query
For you reference check this UNION

In a union the number of column need to match. Try
select Sublessee_uname, Sublessee_fname, Sublessee_mname, Sublessee_fname
from sublessee
union
select Sublessee_space, Sublessee_product, null, null
from space_reserve
Simplified Fiddle example

Based on some of your comments, it seems to me that you need a join of two tables rather than a union thereof.
And often, to join two tables, one of them must have a column or columns referencing the other. As the columns shown in your query do not seem to contain among them the right ones to use in the join condition and I don't know at this point what other columns your two tables possess, I'm going two assume that there's a Sublessee_Id column in both tables. And the query to retrieve the required data would then look something like this:
SELECT
s.Sublessee_uname,
s.Sublessee_fname,
s.Sublessee_mname,
s.Sublessee_fname,
r.Sublessee_space,
r.Sublessee_product
FROM sublessee s
LEFT JOIN space_reserve r /* or, depending on the requirements,
INNER JOIN space_reserve r */
ON s.Sublessee_Id = r.Sublessee_Id
;
If you want to learn more about JOIN syntax, you can try this manual page:
JOIN Syntax (MySQL)

Related

MySQL - removing ID columns used for joining

I have 3 tables of data I'm joining together. I'm using an ID column (day) in each table to join the data together. The following SQL query works, and provides the following column set:
SELECT *
FROM records
LEFT JOIN macro_nutrients ON macro_nutrients.day = records.day
LEFT JOIN micro_nutrients ON micro_nutrients.day = records.day
day|date|calories_in|calories_out|day|fat|sugar|protein|day|iron|cholesterol|potassium|vitamin_a|vitamin_c|
As I join tables, the "day" field (which works as my primary key for joining tables) gets outputted each time (3 tables would have three day fields listed). I would like to exclude the day fields from my column set, as they aren't needed.
Would this only be achievable by explicitly mentioning the fields I want in my select statement (eg. macro_nutrients.fat)? this would unfortunately get quite verbose if so. Curious if there a more compact way.
You can use USING:
SELECT *
FROM records r LEFT JOIN
macro_nutrients man
USING (day) LEFT JOIN
micro_nutrients mn
USING (day);
USING is SQL standard syntax that both SQLite and MySQL support. When * is used with USING, the columns used as JOIN keys are not duplicated in the result set. In an outer join, the value is the actual value in the data -- rather than the unmatched NULL value.

SQL query giving repetitive results

I have two tables and I want to fetch select columns from the tables.
table 1 is sfpinventoryinfo and table 2 is opticalportinfo.
Both have NEID as common.
SELECT
sfpinventoryinfo.NEID,
sfpinventoryinfo.SlotNumber,
sfpinventoryinfo.PortNo,
sfpinventoryinfo.PortType,
sfpinventoryinfo.`Type`,
sfpinventoryinfo.SN,
sfpinventoryinfo.GenDes,
sfpinventoryinfo.ApplicationCode,
opticalportinfo.ChannelFrequency
FROM
sfpinventoryinfo
JOIN
opticalportinfo ON sfpinventoryinfo.NEID = opticalportinfo.NEID;
But I am getting weird results:
As shows above result, Slot no 4 should have only 1 entry for port instead of 5
It's likely your opticalportinfo has six rows with the value 13 in NEID. So, your join produces all six rows in your result set.
It's hard to guess the "right" way to choose which of those six rows to use without knowing more about your application. You can hack around the problem with SELECT DISTINCT if you must. But it's a hack.
You clearly have duplicates in one or both tables. In your example data, the entire row looks duplicated, so you could use select distinct so entire rows are not repeated:
SELECT DISTINCT i.NEID, i.SlotNumber, i.PortNo, i.PortType, i.`Type`, i.SN,
i.GenDes, i.ApplicationCode, oi.ChannelFrequency
FROM sfpinventoryinfo i JOIN
opticalportinfo op
ON i.NEID = oi.NEID;
Or perhaps GROUP BY:
SELECT i.NEID, i.SlotNumber, i.PortNo, i.PortType, i.`Type`, i.SN,
i.GenDes, i.ApplicationCode, MAX(oi.ChannelFrequency)
FROM sfpinventoryinfo i JOIN
opticalportinfo op
ON i.NEID = oi.NEID
GROUP BY i.NEID, i.SlotNumber, i.PortNo, i.PortType, i.`Type`, i.SN,
i.GenDes, i.ApplicationCode;
That said, you really need to understand why there are duplicates and adjust your query or fix your data.

comparing two table columns in mysql results in duplicate

I have two tables, I've been trying to print the result from each but they are being duplicated. These are the two MySQL tables and the result. Notice the duplication.
The sql code for the project is:
SELECT * FROM savings,savtype WHERE cust_id=".$_SESSION['user']
I'm also looking for a work around this, in the meantime, id appreciate any assistance on this.
because you are not specifying how the two tables are related. You need to add that, either via an explicit ... JOIN ... (USING|ON)
SELECT
*
FROM
savings JOIN savtype USING (savtype_id)
WHERE
cust_id = ".$_SESSION['user']
or by providing the criteria in the where clause.
SELECT
*
FROM
savings, savtype
WHERE
savings.savtype_id = savtype.savtype_id AND
cust_id = ".$_SESSION['user']
As I understand from the screenshot you added, it makes joint between those tables, and what you probably want it left join from savings and savtype tables.
SELECT *
FROM `savings`
LEFT JOIN `savtype`
ON savings.savtype_id=savtype.savtype_id
where cust_id=".$_SESSION['user'] .";
Update if this did the trick,
You can learn more about left join here: https://www.w3schools.com/sql/sql_join_left.asp

mysql getting difference of 2 result sets

So I have two tables. One is called superIDs which has columns "superID BIGINT" and I have another one called mainIDs which has a column called "subid BIGINT". I know that mainIDS is a subset of superIDs. How can I see the rows that are ONLY in superID but not in mainIDs.
here is my attempt at a solution:
SELECT * FROM atlas_comparables.superIDs WHERE NOT EXISTS
(SELECT * FROM atlas_comparables.mainIDs);
However, this is returning me an empty set. Any help?
Try this
SELECT * FROM atlas_comparables.superIDs WHERE the_id_column NOT IN
(SELECT the_idcolumn_ofcomparable FROM tlas_comparables.mainIDs);
Note: the_id_column is in superIDs table
the_idcolumn_ofcomparable is in MainIdstable
If the ids only appear (at most) once in each table, then you can do this relatively easily with a left outer join:
select si.*
from atlas_comparables.superIDs si left join
atlas_comparables.mainIDs mi
on si.SuperID = mi.SubID
where mi.SubId is NULL;
If you are trying to compare all the columns (as except does in other databases), then you need a more complicated query, where you include all the columns on the on clause.

Querying multiple mySQL tables simultaneously

as the title states, I am trying to query multiple mySQL tables in the same DB simultaneously, here is my query,
SELECT * FROM `database_db`.`one, two, three, four, five, six, seven` WHERE uid='1234567'
I am using mySQL workbench to do the query, but when I run it I get this error
Error Code: 1103
Incorrect table name 'one, two, three, four, five, six, seven'
I know that the problem lies with my query, where I select the tables, I am assuming that I cannot select more than one, or maybe my syntax is incorrect?
Is there another way for me to achieve the same result?
EDIT: Each table is different, each has a different amount of columns, the only thing they have in common is the uid.
For instance, table one has the columns "oneA, oneB, oneC" and table two has the columns "twoA, twoB, twoC, twoD, twoE", and so on and so forth for each table, as you can see they do not have the same amount of columns, and are in no way identical to each other.
Thanx in advance.
You probably want to JOIN the tables:
SELECT *
FROM one
JOIN two ON one.uid = two.uid
JOIN three ON one.uid = three.uid
WHERE one.uid='1234567'
IF table structure is identical for tables: one, two, three ... then you can use UNION
SELECT * FROM one
WHERE uid='1234567'
UNION ALL
SELECT * FROM two
WHERE uid='1234567'
UNION ALL
SELECT * FROM three
WHERE uid='1234567'
....
UNION ALL
SELECT * FROM seven
WHERE uid='1234567'
Maybe I don't understand the question very well but what about something like this:
SELECT *
FROM table_a a, table_b b, table_c c
WHERE a.uid = b.uid = c.uid = 12345;