Hi everybody I need done help with this issue.
Example myDb tabele name people
ID name Sex
3 Adam Smith M
5 Sonia Kolan F
3 Donald Smith M
Select id, name from people where name LIKE ‚%Smith%’;
Result
Id Name
3 Adam Smith
3 Donald smith
My question is how to transform my result of col name to this view
Id name
3 Smith
3 Smith
I want too see in col name Expresion from like statement
I want too see in col name Expresion from like statement
Then just put that literal string in the select clause:
select id, 'Smith' name
from people
where name like '%Smith%'
You can join to the table a query that returns only 'Smith':
select p.id, t.name
from people p
inner join (select 'Smith' name) t
on p.name LIKE concat('%', t.name, '%');
See the demo.
Results:
> id | name
> -: | :----
> 3 | Smith
> 3 | Smith
Related
Before I have asked the same problem (Join table with comma issue (MySQL)) about join table with comma in the column.
I have two tables, table structure like below:
First Table name: student
id | name | course_id
——————————————————————————
1 David 1,3
2 Peter 2,4
3 Shawn 2,6
Second Table name: subject
id | subject
———————————————————
1 English
2 Maths
3 Science
4 Geographic
5 Accounting
6 Art & Design
I have tried this find_in_set method (Search with comma-separated value mysql), but it cannot get the actual result. I want the actual result is like below:
id | name | subject_name
——————————————————————————
1 David English,Science
2 Peter Maths,Geographic
3 Shawn Maths,Art & Design
I am using below code:
SELECT student.id as id,student.name as name,student.subject as subject_name
FROM student
INNER JOIN subject
ON FIND_IN_SET(subject.id, student.course_id) > 0
But the result is shown me like below:
id | name | subject_name
——————————————————————————
1 David English
2 David Science
3 Peter Maths
4 Peter Geographic
5 Shawn Maths
6 Shawn Art & Design
Hope someone guide me on how to solve this problem. Thanks.
Like this
SELECT student.id as id, student.name as name, GROUP_CONCAT(subject.subject) as subject_name
FROM student
INNER JOIN subject
ON FIND_IN_SET(subject.id, student.course_id) > 0
GROUP BY student.id, student.name
Usually we don't concat everything in SQL query, but you can do
SELECT CONCAT_WS(' ', student.id, student.name, GROUP_CONCAT(subject.subject)) as concated_value
FROM student
INNER JOIN subject
ON FIND_IN_SET(subject.id, student.course_id) > 0
GROUP BY student.id, student.name
I have a table in which I want to compare an ID column with another column in the same table.
This is my table
ID | Name | BossID
1 John 3
2 Max 4
3 Peter 4
4 Alex 5
For example I want to use
select * from mytable where ID = BossID
and I expected to get that Peter is the Boss of John and Alex is the Boss of Max and Peter, but when I use it this way, I dont get any info on the query...
Any idea to get it?
The expected query result are:
ID | Name | BossID
1 John 3
2 Max 4
3 Peter 4
seems like you want the list of employees who their boss exists in the list as well:
select *
from mytable
where bossid in (select id from mytable);
to get the boss name :
select t.ID, t.Name, b.Name as BossName
from mytable t
join mytbale b
on t.bossid = b.id
you can use left join to return all the employees in the list
I have a table with columns id, user
I want to group by column id and show a list of users (comma separated) for each id.
In the final output I need to display:
user joe - id 1
users jim, mark, john - id 2
user dave - id 3
....
I'm getting error "Cardinality violation: 1242 Subquery returns more than 1 row" if I try this:
SELECT id, (SELECT distinct(user) FROM mytable b where a.id = b.id)
FROM mytable a
GROUP BY id
Since you are using MySQL, there is a built-in function for that which is the GROUP_CONCAT function. For example, you have a records like this:
ID User
1 Joe
2 Jim
2 Mark
2 John
3 Dave
If you try to run this query:
SELECT ID, GROUP_CONCAT(User) Users
FROM tableName
GROUP BY ID
you will then have a result like this:
ID Users
1 Joe
2 Jim, Mark, John
3 Dave
What you're looking for is the GROUP_CONCAT aggregate function:
SELECT id, GROUP_CONCAT(user) FROM table GROUP BY id
I am trying to find duplicates by comparing the first name and surname columns in a table. The first name can be a name or an initial.
Reading other posts I have managed to figure out how to get the duplicate surnames and list the first letter for first name. But I am unsure how to only show rows where there is a match of surname and the first letter of the first name.
SELECT *
FROM table AS a
INNER JOIN (
SELECT LEFT( firstname, 1 ) , surname
FROM table
GROUP BY surname
HAVING COUNT( * ) > 1
) AS b ON a.surname = b.surname
id | firstname | surname
**************************
1 | joe | bloggs
2 | j | bloggs
3 | s | bloggs
4 | f | doe
5 | frank | spencer
Currently this query would return
1 | joe | bloggs
2 | j | bloggs
3 | s | bloggs
Result I would like would just contain the possible duplicates.
1 | joe | bloggs
2 | j | bloggs
I don't quite get what you want. Yor provided a query, your current table and the expected result.
I've just created your table, run your query and got the expected result. What is wrong with this?
SELECT FROM table1 AS a
INNER JOIN (
SELECT surname FROM table1
GROUP BY surname
HAVING COUNT(*) > 1
) AS b ON a.surname = b.surname
This effectively result in your expected result:
joe | bloggs
j | bloggs
Or am I missing something?
After re-reading... are you expecting to get only this?
j | bloggs
If that is the case, use this:
SELECT * FROM table1 AS a
INNER JOIN (
SELECT surname FROM table1
GROUP BY surname
HAVING COUNT(*) > 1
) AS b ON a.surname = b.surname
WHERE CHAR_LENGTH(firstname) = 1
Edit:
After the expected result was properly explained I conclude the query should be:
SELECT a.firstname, a.surname FROM t1 AS a
INNER JOIN (
SELECT LEFT(firstname, 1) AS firstChar, surname FROM t1
GROUP BY surname, firstChar
HAVING COUNT(surname) > 1
) AS b ON a.surname = b.surname AND b.firstChar = LEFT(a.firstname, 1)
Working example
You probably don't want to use initials all the time, e.g., if you always strip to initials you might consider Bob X the same as Bill X.
So you need to check three cases.
both firstnames are initials
both firstnames are non initials
only one firstname is an intial
So you can work with string methods of Mysql to check the length of either firstname and check the proper case.
I would join the table to itself like so:
select * into #temp from (
SELECT 1, 'joe', 'bloggs' UNION
SELECT 2, 'j', 'bloggs' UNION
SELECT 3, 'f', 'doe' UNION
SELECT 4, 'frank', 'spencer' UNION
SELECT 5, 'steven', 'woo' UNION
SELECT 6, 'steve', 'woo' UNION
SELECT 7, 'stanley', 'woo'
) x (id, firstname, surname)
select
*
from
#temp l
inner join
#temp r
on
left(l.firstname, 1) = left(r.firstname, 1)
and
l.surname = r.surname
where
l.id < r.id
drop table #temp
the downside to this is that the steven and stanley match. I would suggest you think about creating a firstname alias table and use that to standardize the firstnames.
I'd like to have a query for the first and last name on an enrollment list so that only one result show. However, if only the last name is chosen in the query multiple answers will show.
You can GROUP BY the last name field in your query.
For example if you had this data:
MyTable:
id last_name first_name
1 Smith John
2 Smith Jane
3 Jones Paul
Running a query like this:
SELECT t.last_name
FROM MyTable t
GROUP BY t.last_name
ORDER BY t.last_name
...would return these two rows:
Jones
Smith