i have a little problem figuring out what request (MySQL) would suit this requirements:
Table1
+++++++++++++++++
id| name
+++++++++++++++++
1 | John Smith
2 | Eric Smith
3 | Martha Smith
4 | Smith
5 | Ronald Smith Donald
6 | Marc Fisher
Table2
+++++++++++++++++++++++
regex | value1 | value2
+++++++++++++++++++++++
Smith | Mister | 356
Fisher| Sir | 24
To select result values as
Result Set
+++++++++++++++++++++++++++++++++++++++++++++++++++
id| name | regex | value1 | value2
+++++++++++++++++++++++++++++++++++++++++++++++++++
1 | John Smith | Mister | 356
2 | Eric Smith | Mister | 356
3 | Martha Smith | Mister | 356
4 | Smith | Mister | 356
5 | Ronald Smith Donald | Mister | 356
6 | Marc Fisher | Sir | 24
Thanks in advance.
You could use MySQL's regexp:
select *
from Table1 t1
join Table2 t2
on t1.name regexp t2.regex
Funny things will occur if a name matches more than one regex in Table2.
Related
I'm not really sure how to ask this and I've been search-engining for awhile and haven't come up with anything useful.
Say I have the following three tables People, Cities and States:
PeopleID | Name | Age | CityIDFK
--------------------------------------------
1 | John | 24 | 20
2 | Jim | 28 | 21
3 | Joan | 49 | 10
4 | Mike | 37 | 10
5 | Bruce | 26 | 2
6 | Peter | 22 | 20
7 | Oprah | 27 | 3
7 | Jake | 21 | 1
CityIDPK | City | StateIDFK
---------------------------------------
1 | Seattle | 1
2 | Gotham | 2
3 | Oakland | 4
10 | Boise | 5
20 | Austin | 6
21 | Tyler | 6
StateIDPK | StateName
----------------------------
1 | Washington
2 | New York
3 | Oregon
4 | California
5 | Idaho
6 | Texas
How can I achieve the following output:
StateName | City | Name
---------------------------------------
California | Oakland | Oprah
Idaho | Boise | Mike
| | Joan
New York | Gotham | Bruce
Washington | Seattle | Jake
Texas | Austin | John
| | Peter
| Tyler | Jim
I'm not sure if the above output is possible or not, I would probably just do something like this:
SELECT StateName, City, Name FROM People
INNER JOIN Cities
ON Cities.CityIDPK = People.CityIDFK
INNER JOIN States
ON States.StateIDPK = Cities.StateIDFK
But that would return the StateName and City for every person, instead of just once.
StateName | City | Name
---------------------------------------
California | Oakland | Oprah
Idaho | Boise | Mike
Idaho | Boise | Joan
New York | Gotham | Bruce
Washington | Seattle | Jake
Texas | Austin | John
Texas | Austin | Peter
Texas | Tyler | Jim
If the output I want to achieve is possible, would someone show me an example of how to write the query, or point me in the right direction?
I agree with Tim, this should be handled in your presentation layer.
But you can do it if you're using MySQL 8 by taking advantage of window functions. We can use row_number() to determine the first row per state and per city, then use a case to only display the name for the first row.
SELECT
case row_number() over states_w
when 1 then states.name
else '' end as StateName,
case row_number() over cities_w
when 1 then cities.name
else '' end as CityName,
people.name as Name
FROM People
INNER JOIN Cities ON Cities.id = People.City_id
INNER JOIN States ON States.id = Cities.state_id
window states_w as (partition by states.id),
cities_w as (partition by cities.id);
If you're using MySQL before 8... upgrade. If you can't upgrade, it can be emulated.
I have database that contains some distinct values like below
id firstName lastName someOption
1 mark tom 28
2 jack bob 75
3 mark tom 48
4 mark tom 87
5 sara tim 64
6 jack bob 23
7 katy jimmy 65
I want to select all the table but filter out distinct records instead of the last one like this
id firstName lastName someOption
4 mark tom 87
5 sara tim 64
6 jack bob 23
7 katy jimmy 65
How to achieve this with sql?
One method is a correlated subquery:
select t.*
from t
where t.id = (select max(t2.id)
from t t2
where t2.firstname = t.firstname and t2.lastname = t.lastname
);
With an index on (lastname, firstname, id), this is probably the fastest method on larger amounts of data.
With NOT EXISTS:
select t.* from tablename t
where not exists (
select 1 from tablename
where (firstName, lastName) = (t.firstName, t.lastName) and id > t.id
)
See the demo.
Results:
| id | firstName | lastName | someOption |
| --- | --------- | -------- | ---------- |
| 4 | mark | tom | 87 |
| 5 | sara | tim | 64 |
| 6 | jack | bob | 23 |
| 7 | katy | jimmy | 65 |
I want a count of the number of rows for each user in the table. For example, suppose I have a table like this:
user_id | order_id | name
--------+----------+-----
6 | a1 | Joe
6 | b4 | Joe
6 | c2 | Joe
6 | d5 | Joe
6 | a6 | Joe
8 | b9 | Mary
8 | c7 | Mary
8 | a2 | Mary
3 | ba | Jack
3 | c3 | Jack
3 | a9 | Jack
3 | b6 | Jack
5 | c9 | Jill
5 | d2 | Jill
I want a result that tells me that Joe appears 5 times, Mary 3 times, Jack 4 times, and Jill twice. So I want a sql statement (preferably mySql, but any language will do), that gives results like this:
name | user_id | count
-----+---------+------
Joe | 6 | 5
Mary | 8 | 3
Jack | 3 | 4
Jill | 5 | 2
Is there a way to do this in sql? I can't find one, but there's a lot I don't know about sql.
Select
Count(userid) as count
, userid
, name
From table
Group by userid, name
Please try:
SELECT COUNT(order_id) AS count, name FROM table_name GROUP BY name
I have two tables I am trying to match. I want to have the referredbyname in sourcelist match the name column to targetjoin when the record is in the same groupname
I would like to join these two tables with one-one match. Contacts in targetjoin have multiple recordtypes with the same name. I want to do some type of distinct join based on a hierarchy set up of record type name
For example: If a record is matched with the two recordtypes for the same name the RecordType: TypeA will be matched only, and so forth.
sourcelist :
ID GroupName Name ReferredbyName
1 A John Smith Sally Bosh
2 A Craig Miller Sally Smith
3 A Fulton Fork Spoon Knife
4 B Joe Sample George Test
targetjoin :
ID GroupName Name RecordType
101 A Sally Bosh TypeA
102 A Sally Bosh TypeB
103 A Sally Smith TypeC
104 A Sally Smith TypeD
105 B George Test TypeF
My Result:
| id | groupname | name | referredbyname | id | groupname | name | recordtype |
|----|-----------|--------------|----------------|-----|-----------|-------------|------------|
| 2 | A | Craig Miller | Sally Smith | 103 | A | Sally Smith | TypeC |
| 1 | A | John Smith | Sally Bosh | 102 | A | Sally Bosh | TypeB |
| 1 | A | John Smith | Sally Bosh | 101 | A | Sally Bosh | TypeA |
| 2 | A | Craig Miller | Sally Smith | 104 | A | Sally Smith | TypeD |
| 4 | B | Joe Sample | George Test | 105 | B | George Test | TypeF |
This result gives me all the of possible matches one-many join with duplicate ID
I would like to have desired result like this:
| id | groupname | name | referredbyname | id | groupname | name | recordtype |
|----|-----------|--------------|----------------|-----|-----------|-------------|------------|
| 2 | A | Craig Miller | Sally Smith | 103 | A | Sally Smith | TypeC |
| 1 | A | John Smith | Sally Bosh | 101 | A | Sally Bosh | TypeA |
| 4 | B | Joe Sample | George Test | 105 | B | George Test | TypeF |
This is what I have gotten so far
select a.*, b.*
from sourcelist a
join targetjoin b
on a.groupname=b.groupname
and
case
when b.recordtype in ('TypeA') and a.referredbyname=b.name then 1
when b.recordtype in ('TypeB') and a.referredbyname=b.name then 2
when b.recordtype in ('TypeC') and a.referredbyname=b.name then 3
when b.recordtype in ('TypeD') and a.referredbyname=b.name then 4
when b.recordtype in ('TypeE') and a.referredbyname=b.name then 5
when b.recordtype in ('TypeF') and a.referredbyname=b.name then 6
else 0
end in (1,2,3,4,5,6)
order by a.groupname
Schema: http://sqlfiddle.com/#!9/eb97f
Thanks for any help!
SELECT x.*
FROM targetjoin x
JOIN
( SELECT name
, MIN(recordtype) recordtype
FROM targetjoin
GROUP
BY name
) y
ON y.name = x.name
AND y.recordtype = x.recordtype;
The last part of this problem has been left as an exercise for the reader.
Trying to figure out how to display the content of two mysql tables when one table has multiple matching rows, eg. so that the tables below display the results:
London - Harry Smith, Oliver Jones, Jack Taylor
Manchester - Charlie Williams, Jacob Brown
Table One Table two
+-----------------------------+ +----------------------------------+
| id | branchid | store | | branchid | firstname | employee |
+-----------------------------+ +----------------------------------+
| 1 | 1 | London | | 1 | Harry | Smith |
| 2 | 2 | Manchester | | 1 | Oliver | Jones |
+-----------------------------+ | 1 | Jack | Taylor |
| 2 | Charlie | Williams |
| 2 | Jacob | Brown |
+----------------------------------+
Many thanks.
GROUP_CONCAT will do the job, although we first need to CONCAT as well to put firstname and lastname together:
SELECT b.store, GROUP_CONCAT(CONCAT(e.firstname, ' ', e.employee)) AS Employees
FROM TableOne b INNER JOIN TableTwo e on b.branch = e.branchid
GROUP BY b.store;
SqlFiddle here
Result:
Store Employees
London Harry Smith,Jack Taylor,Oliver Jones
Manchester Jacob Brown,Charlie Williams`