mysql query - how to merge tables in another table - mysql

I have a problem joining tables in the result column. i have a working query which combine different tables using UNION but when i'm extending another table i got an error saying 'The used SELECT statements have a different number of columns'
this is my query:
(SELECT
IDNumber,
CONCAT(LastName, ', ', FirstName, ' ', Middle) as Name,
CONCAT(EmDesignation, ', ', Department) as Information,
Image,
v.PlateNo as PlateNumber
FROM
tblemployee as e, tblvehicle as v
WHERE
v.RFIDNo LIKE '6424823943'
AND
e.RFIDNo LIKE '6424823943')
UNION
(SELECT
IDNumber,
CONCAT(LastName, ', ', FirstName, ' ', Middle) as Name,
CONCAT(Course, ', ', Year) as Information,
Image,
v.PlateNo as PlateNumber
FROM
tblstudents as s, tblvehicle as v
WHERE
v.RFIDNo LIKE '6424823943'
AND
s.RFIDNo LIKE '6424823943')
I have problem with this. Continuation query above
UNION
(SELECT
Barrier
FROM
tblinformation as inf
WHERE
inf.RFIDNo IN (6424823943)
ORDER BY
AttendanceNo DESC LIMIT 1)

The error message is correct. Add NULLs to your second query to match the column number, and it will work.
For example:
SELECT
Barrier,
NULL,
NULL,
NULL,
NULL
FROM
tblinformation as inf
...

The error message states what the problem is. Just improve number of columns in SELECT and it will work correctly.

Related

The order of execution for the 'HAVING' clause, without 'GROUP BY' in MySQL

I understand that since the WHERE clause is executed before the SELECT, the following query in MySQL 5 returns an Unknown column error:
SELECT id,
CASE
WHEN LENGTH(TRIM(middle_name)) > 0 THEN
CONCAT(first_name, ' ', LEFT (middle_name, 1), '. ', last_name)
ELSE CONCAT(first_name, ' ', last_name)
END AS name
FROM people_table
WHERE LENGTH(name) < 11;
But using the HAVING clause works:
SELECT id,
CASE
WHEN LENGTH(TRIM(middle_name)) > 0 THEN
CONCAT(first_name, ' ', LEFT (middle_name, 1), '. ', last_name)
ELSE CONCAT(first_name, ' ', last_name)
END AS name
FROM people_table
HAVING LENGTH(name) < 11;
So is the HAVING executed after SELECT, when there is no GROUP BY?
Also I tried in PostgresSQL. Then it looks like that HAVING without GROUP BY here is not allowed. But PostgresSQL does not care about the order of WHERE so the following query works:
SELECT id,
CASE
WHEN LENGTH(TRIM(middle_name)) > 0 THEN
first_name || ' '|| LEFT(middle_name, 1) || '. '|| last_name
ELSE first_name || ' '|| last_name
END AS name
FROM people_table
WHERE LENGTH(name) < 11;
According to the documentation for the SELECT statement, the ability to refer to the SELECT list from HAVING is supported as a non-standard extension to the SQL standard. This shows that HAVING is executed after some parts of the SELECT list by design.
The SQL standard requires that HAVING must reference only columns in the GROUP BY clause or columns used in aggregate functions. However, MySQL supports an extension to this behavior, and permits HAVING to refer to columns in the SELECT list and columns in outer subqueries as well.
However, window functions can be included in the SELECT list and these are documented as being executed after HAVING.
Window functions are permitted only in the select list and ORDER BY clause. Query result rows are determined from the FROM clause, after WHERE, GROUP BY, and HAVING processing, and windowing execution occurs before ORDER BY, LIMIT, and SELECT DISTINCT.
The answer is that HAVING can be executed after some parts of the SELECT list and before other parts of the SELECT list.

Mysql show other different cell data with under group by same name

Let's assume I have a table which store register user data, the records might have same registered name but different email, like following:
I want to create a front view to manipulate those data but I don't want those same name show repeatedly, can mysql statement query to output result like
this is the result so far I can do but it can't bind same name into one.
select * from `register`
where `fullname` in (
select `fullname` from `register`
group by `fullname` having count(*) > 1
)
One thing you could do is to do a SELECT DISTINCT on the duplicate row, and make use of the GROUP_CONCAT(); function in MYSQL to concatenate your desired values into one row, and GROUP BY fullname to get the order you wanted.
Note that I am also putting the user ids into a grouped row, so that you can track which ids belong to which name.
SELECT
DISTINCT fullname as full_name,
GROUP_CONCAT(id SEPARATOR ', ') as user_ids,
GROUP_CONCAT(email SEPARATOR ', ') as emails
FROM
tbl_register
GROUP BY
tbl_register.fullname
Working SQL Fiddle
This would be the logical way to do it. Hope this helped. :)
More information on the GROUP_CONCAT(); function here: https://dev.mysql.com/doc/refman/8.0/en/group-by-functions.html#function_group-concat
Try this:
SELECT DISTINCT *duplicate_column* FROM *table_name1* WHERE *col_id* IN (SELECT *cols_to_dusplay* FROM *table_name1* GROUP_BY *duplicate_column*

Mysql Subquery as input second query

I want to calculate distance from mysql record, firstly I get all cordinates and saved as LineString object, but I have error. What is wrong with my sql?
WITH tmp AS
(SELECT GROUP_CONCAT(CONCAT_WS(' ',lat,lng) SEPARATOR ', ') FROM track WHERE vh_id='75' AND DATE(tdate)='2016-06-09' ORDER BY tdate)
SELECT ST_Length(ST_GeomFromText(tmp));
Firstly, MySQL doesn't support the WITH clause; secondly, you should define variables like 10.4 User-Defined Variables.
You can change you sql to this;)
SELECT GROUP_CONCAT(CONCAT_WS(' ',lat,lng) SEPARATOR ', ') INTO #tmp
FROM track
WHERE vh_id='75' AND DATE(tdate)='2016-06-09' ORDER BY tdate;
SELECT ST_Length(ST_GeomFromText(#tmp));
Or just with one query:
SELECT ST_Length(ST_GeomFromText(GROUP_CONCAT(CONCAT_WS(' ',lat,lng) SEPARATOR ', ')))
FROM track
WHERE vh_id='75' AND DATE(tdate)='2016-06-09' ORDER BY tdate;

Getting Full Name Query

I'm trying to get employee's full name and combine them using MySQL function "Concat". Some of our employee don't have middle name and in this case SQL throws an error. How can i get full name of an employee even if the employee doesn't have middle initial.
SELECT CONCAT(`Employee`.`F_NAME`,
' ',
LEFT(`Employee`.`M_NAME`, 1),
'. ',
`Employee`.`L_NAME`)
FROM `Employee`
Try to use IFNULL
SELECT CONCAT(`Employee`.`F_NAME`,
' ',
IFNULL(CONCAT(LEFT(`Employee`.`M_NAME`, 1),'. '),''),
`Employee`.`L_NAME`)
FROM `Employee`

SQL: List unique substring from fields in table

I'm running a query to retrieve the first word from a string in a colum like so..
SELECT SUBSTRING_INDEX( `field` , ' ', 1 ) AS `field_first_word`FROM `your_table`
But this allows duplicates, what do I need to add to the statement to get unique list of words out?
DISTINCT
For example:
SELECT DISTINCT SUBSTRING_INDEX( `field` , ' ', 1 ) AS `field_first_word` FROM `your_table`
Note: There are performance implications for DISTINCT. However, in your case, there is likely limited pure MySQL alternatives.
To remove duplicates in a SELECT statement, change to SELECT DISTINCT column. In this case:
SELECT DISTINCT SUBSTRING_INDEX( `field` , ' ', 1 ) AS `field_first_word`FROM `your_table`