Combining data in sql - mysql

I have a query that gives me this data:
| id | job | firstName | lastName |
+----+------------+-----------+----------+
| 1 | Programmer | NULL | NULL |
| 2 | NULL | Tom | Tucker |
But I need the table to look like this:
| id | job | firstName | lastName |
+----+------------+-----------+----------+
| 1 | Programmer | Tom | Tucker |
I need for it to display like this, not change the data in the database.

Use aggregate functions. Try this,
select min(Id) as Id,max(job) as Job,max(FNAME) as FNAME,max(LName) as LNAME
from yourtable

Related

MYSQL make a table group of collection

im not sure how i should write for the title. But i've found a case as below:
table name: student
| ID | FAVORITE | NAME | BIRTHDATE |
|----|----------|--------|-----------|
| | MATH | JOHN | 12/06 |
| | CHEM | RYAN | 11/07 |
| | MATH | OODA | 4/09 |
| | HISTORY | YINYIN | 25/05 |
how to make a new table that grouping the name based on their favorite lesson with mysql?
table name: favorite
| FAVORITE | NAME |
|----------|------------|
| MATH | JOHN, OODA |
| CHEM | RYAN |
| HISTORY | YINYIN |
We can use an aggregation query with the help of GROUP_CONCAT():
SELECT FAVORITE, GROUP_CONCAT(NAME) AS NAME
FROM yourTable
GROUP BY FAVORITE;

Retrieve a value from a JOIN as if it was in the base table

Let's say I have two tables, one with firstnames, and another with lastnames:
firstnametable
---------------------------------------------
| id | firstname | |
---------------------------------------------
| 1 | John | |
| 2 | Pete | |
---------------------------------------------
lastnametable
---------------------------------------------
| id | lastname | firstname_id | active |
---------------------------------------------
| 1 | Dough | 1 | 0 |
| 2 | Do | 1 | 1 |
| 2 | Sampres | 2 | 1 |
---------------------------------------------
I am looking for Query such that it would appear as if it came from a single table:
firstnametable
---------------------------------------------
| id | firstname | lastname | |
---------------------------------------------
| 1 | John | Do | |
| 2 | Pete | Sampres | |
---------------------------------------------
But it is important for me that the results are delivered in such a fashion that the prefix is also as if the results are all from the original table, ie:firstnametable.firstname and firstnametable.lastname
Is this at all possible?
My current solution is:
SELECT firstnametable.id,
firstnametable.firstname,
CONCAT(jointable.name) as lastname
FROM firstnametable
LEFT JOIN (
SELECT lastname
FROM lastnametable
WHERE active = 1
) as jointable ON (firstnametable.id = jointable.firstname_id)
But I am still missing my desired firstnametable prefix for the lastname column.
You need to include the firstname id in the jointtable select and I don't know what the purpose of the concat is since you aren't concating anything but you haven't selected name anywhere perhaps you meant lastname. If you want to include the table name in the headers use an alias.
SELECT firstnametable.id as 'firstnametable.id',
firstnametable.firstname as 'firstnametable.firstname',
jointable.lastname as 'firstnametable.lastname'
FROM firstnametable
LEFT JOIN (
SELECT firstname_id,
lastname
FROM lastnametable
WHERE active = 1
) as jointable ON firstnametable.id = jointable.firstname_id;
+-------------------+--------------------------+-------------------------+
| firstnametable.id | firstnametable.firstname | firstnametable.lastname |
+-------------------+--------------------------+-------------------------+
| 1 | John | Do |
| 2 | Pete | Sampres |
+-------------------+--------------------------+-------------------------+
2 rows in set (0.001 sec)

Finding MySQL near-duplicates across two columns using wildcards

I have a table with id, first_name and last_name columns. I'd like to get a listing of rows where last_name and the first character of first_name are duplicated. I am groping my way around and have a sense that there is a COUNT('WHERE') in there, but can't quite get to it.
In essence, I'm looking for possible duplicates. So, from this subset:
+------+-----------+-----------+-------------+------------+
| id | firstName | lastName | dateOfBirth | createdOn |
+------+-----------+-----------+-------------+------------+
| 143 | Susie | Wong | 2015-12-01 | 2016-07-11 |
| 1268 | Dale | Armstrong | 2017-01-01 | 2017-01-04 |
| 1435 | Olive | Armstrong | 1941-03-11 | 2017-03-08 |
| 2013 | Timotini | Attilio | 1932-01-01 | 2017-08-21 |
| 2014 | Olinda | Attilio | 1938-01-01 | 2017-08-21 |
| 3076 | Sue | Armstrong | 1951-06-01 | 2018-06-22 |
| 3079 | Susan | Armstrong | 1951-09-15 | 2018-06-22 |
+------+-----------+-----------+-------------+------------+
I would like a query that returns only 3076 and 3079 (Sue and Susan Armstrong) based on looking for a matching last name and a matching first initial, like so:
+------+-----------+-----------+-------------+------------+
| id | firstName | lastName | dateOfBirth | createdOn |
+------+-----------+-----------+-------------+------------+
| 3076 | Sue | Armstrong | 1951-06-01 | 2018-06-22 |
| 3079 | Susan | Armstrong | 1951-09-15 | 2018-06-22 |
+------+-----------+-----------+-------------+------------+
Here's one option using exists and left:
select *
from yourtable y
where exists (
select 1
from yourtable y2
where y.id != y2.id
and y.lastname = y2.lastname
and left(y.firstname,1) = left(y2.firstname,1)
)
Sample Fiddle Demo
Duplicates of last_name
SELECT id, first_name, last_name, COUNT(*) c
FROM table
GROUP BY last_name
HAVING c > 1;
For grouping by the first character in first_name, try playing with left() function

How select remaining unspecified columns

I am looking to overwrite a column name in a table with an existing column name.
I am Looking for a way to get the remaining unspecified columns in the tables.
Note:
The query could have more joins in the future.
eg
Person
+-----------+----------+---------+
| firstname | lastname | pers_id |
+-----------+----------+---------+
| Joe | Soap | 1 |
| Bobby | Pin | 2 |
| Janet | Jackson | 3 |
+-----------+----------+---------+
Category
+----------+-------------------+--------+
| type | description | cat_id |
+----------+-------------------+--------+
| customer | people who pay us | 1 |
| employee | people we pay | 2 |
| director | people who direct | 3 |
+----------+-------------------+--------+
Person_Cat
(=^ェ^=)
+---------+--------+
| pers_id | cat_id |
+---------+--------+
| 3 | 1 |
| 2 | 2 |
| 1 | 3 |
+---------+--------+
Query
SELECT *, CONCAT(p.firstname, ' '
, p.lastname) as full_name
, c.cat_id AS category_id
, p.pers_id AS cat_id
FROM Person AS p
JOIN Person_Cat AS pc ON(p.pers_id = pc.pers_id)
JOIN Category AS c ON (pc.cat_id = c.cat_id)
OUTPUT
(Apologies for the length but the table after is more important)
+-----------+----------+---------+---------+--------+----------+-------------------+--------+---------------+-------------+--------+
| p | p | p | pc | pc | c | c | c | Select | Select | Select |
+-----------+----------+---------+---------+--------+----------+-------------------+--------+---------------+-------------+--------+
| firstname | lastname | pers_id | pers_id | cat_id | type | description | cat_id | full_name | category_id | cat_id |
+-----------+----------+---------+---------+--------+----------+-------------------+--------+---------------+-------------+--------+
| Janet | Jackson | 3 | 3 | 1 | customer | people who pay us | 1 | Janet jackson | 1 | 3 |
| Bobby | Pin | 2 | 2 | 2 | employee | people who we pay | 2 | Bobby Pin | 2 | 2 |
| Joe | Soap | 1 | 1 | 3 | director | people who direct | 3 | Joe Soap | 3 | 1 |
+-----------+----------+---------+---------+--------+----------+-------------------+--------+---------------+-------------+--------+
The headers above column names are there for reference
to where they comes from.
Column summary -
firstname, lastname, pers_id, pers_id, cat_id, type,
description, cat_id, full_name ,category_id, cat_id
Wanted output
+-----------+----------+---------+--------+----------+-------------------+---------------+-------------+--------+
| p | p | pc | pc | c | c | Select | Select | Select |
+-----------+----------+---------+--------+----------+-------------------+---------------+-------------+--------+
| firstname | lastname | pers_id | cat_id | type | description | full_name | category_id | cat_id |
+-----------+----------+---------+--------+----------+-------------------+---------------+-------------+--------+
| Janet | Jackson | 3 | 1 | customer | people who pay us | Janet jackson | 1 | 3 |
| Bobby | Pin | 2 | 2 | employee | people who we pay | Bobby Pin | 2 | 2 |
| Joe | Soap | 1 | 3 | director | people who direct | Joe Soap | 3 | 1 |
+-----------+----------+---------+--------+----------+-------------------+---------------+-------------+--------+
Column summary -
firstname, lastname, pers_id, cat_id, type,
description, full_name ,category_id, cat_id
Notice:
The p.pers_id and the c.cat_id are not present. I would like to think this would be because the were called directly and unmodified unlike the first and lastname used in ConCat
When the short answer is that there is no such concept as Select [remaining columns]at this time (2015-06-17), if you want to use SELECT * but only remove redundant columns,
then you will need to explicitly remove (ignore) those redundant columns when rendering your view.
You will have to explicitly configure logic of which columns to ignore, which is pretty much the same thing as explicitly listing the columns that you are interested in, so you get back to the argument against selecting all columns that I made in the comments above.
Unless your table schema is changing all the time, there really isn't reason for this.

Mysql query to select only one unique name on a criteria

I have a table like this.
+------------+-------------+--------------+
| name | hobby | hobby_number |
+------------+-------------+--------------+
| jack | sport | 1 |
| marco | skydiving | 3 |
| alfonso | driving | 1 |
| marco | learning | 2 |
| jack | dancing | 2 |
+------------+-------------+--------------+
I want to use sql select statement to select only one unique name.
The table I want may look like this:
+------------+-------------+--------------+
| name | hobby | hobby_number |
+------------+-------------+--------------+
| jack | sport | 1 |
| marco | learning | 2 |
| alfonso | driving | 1 |
+------------+-------------+--------------+
What should sql query be?
Thank you in advance.
select t.* from your_table t
inner join
(
select name, min(hobby_number) as minh
from your_table
group by name
) x on x.name = t.name and x.minh = t.hobby_number