I am trying to add titles or breaks between groups of names in MYSQL. Here's an example.
Output:
Bob Honda 1
Bob Acura 2
Bob Toyota 3
Joe Honda 1
Joe Acura 2
Joe Toyota 3
Jim Honda 1
Jim Acura 2
Jim Toyota 3
What I want is:
Bob
Bob Honda 1
Bob Acura 2
Bob Toyota 3
Joe
Joe Honda 1
Joe Acura 2
Joe Toyota 3
Jim
Jim Honda 1
Jim Acura 2
Jim Toyota 3
or even just this
Bob Honda 1
Bob Acura 2
Bob Toyota 3
Joe Honda 1
Joe Acura 2
Joe Toyota 3
Jim Honda 1
Jim Acura 2
Jim Toyota 3
Is this possible through MySQL?
Thank you in advance.
Suppose initially you have a query like this
select name,
car,
number
from a_table
You can use something like this
select name,
car,
number
from a_table
union all
select name,
'' as car,
1000000000 as number
from a_table
group by name
order by name, number
here grouped results generates the extra lines and then order by fixes the rows orders.
Related
user | manager | permission
-------------------------
adam john 1
adam john 2
adam john 4
dave john 1
dave john 2
dave john 3
dave john 4
ben rick 3
ben rick 4
ben rick 5
ed susan 2
ed susan 3
carl susan 1
carl susan 4
nancy peter 3
nancy peter 4
i want a query which would return user's having permission 2 but not 3 OR having permission 3 but not 2 BUT i DONT want users who have both permissions 2 and 3 and users who dont have both permissions 2 and 3
user
-----
adam
ben
nancy
If you filter for who have permission 2 or 3 you can group and filter for who only have one permission in 2,3
SELECT
user
FROM table
WHERE permission IN (2, 3)
GROUP BY USER
HAVING COUNT(permission) = 1;
You can group by user and check so that the max and min permissions are equal to either 2 or 3:
select user from tablename
where permission in (2,3)
group by user
having
(min(permission) = 2 and max(permission) = 2)
or
(min(permission) = 3 and max(permission) = 3)
I wanna hide id if NOT like but don't work
SELECT *
FROM character, character_actor WHERE character.id NOT LIKE character_actor.character_id;
id name id character_id actor_name
1 Doogie Howser 1 4 Alyson Hannigan
1 Doogie Howser 2 3 Alyson Hannigan
1 Doogie Howser 3 2 Neil Patrick Harris
2 Barney Stinson 1 4 Alyson Hannigan
2 Barney Stinson 2 3 Alyson Hannigan
2 Barney Stinson 4 1 Neil Patrick Harris
3 Lily Aldrin 1 4 Alyson Hannigan
3 Lily Aldrin 3 2 Neil Patrick Harris
3 Lily Aldrin 4 1 Neil Patrick Harris
4 Willow Rosenberg 2 3 Alyson Hannigan
4 Willow Rosenberg 3 2 Neil Patrick Harris
4 Willow Rosenberg 4 1 Neil Patrick Harris
You want a NOT IN query:
SELECT *
FROM character WHERE character.id NOT IN (SELECT character_actor.character_id from character_actor)
It can also be accomplished with a NOT EXISTS. Depending on the DBMS and structure, this might lead to better performance.
SELECT *
FROM character c WHERE NOT EXISTS(SELECT NULL from character_actor ca WHERE ca.character_id=c.id)
Try this query
SELECT *
FROM character as c JOIN character_actor as ca
ON c.character_id != ca.character_id
for example I have two tables , I wanna get two_table.name without one_table.name
one_table
id name
1 Dave
2 Mary
3 Fry male
4 Leela
5 Odie
two_table
id name
1 Dave
2 Mary
3 Fry male
4 Leela
5 Odie
6 Jumpy
7 Sneakers
8 Jack
9 Malia
10 Lee
Having these tables:
clients
-------------------
id
name
town
companies
------------------
id
name
credits
------------------
clients_id
companies_id
credit
Example:
clients
-------------
1 john doe London
2 jane smith Paris
companies
-------------
1 mycompany1
2 mycompany2
credits
-------------
1 1 5000
1 2 3250
2 2 4500
How can I list all the clients including an additional column for each credit she has in a company? I also need to alias each of these columns in this fashion: "credit_"
Something like:
id name town credit_1 credit_2
1 john doe London 5000 3250
2 jane smith Paris NULL 4500
I'm not sure if doing two LEFT JOINS to credits would work.
I’ve been trying to wrangle some data from a group of MySQL tables into a Pandas DataFrame with a MultiIndex. The tables are roughly like this:
create table team (
teamID integer NOT NULL,
teamName varchar(64) NOT NULL,
primary key (teamID));
create table coach (
coachID integer NOT NULL,
teamID integer NOT NULL,
coachName varchar(64) NOT NULL,
primary key (coachID));
create table player (
playerID integer NOT NULL,
teamID integer NOT NULL,
playerName varchar(64) NOT NULL,
primary key (playerID));
Each team can have one or more coaches and one or more players.
Here’s the SELECT and MERGE:
import mysql.connector
connection = mysql.connector.connect(user='root', passwd='temp', database='mydb')
team = sql.read_frame('select * from team;', connection)
coach = sql.read_frame('select * from coach;', connection)
player = sql.read_frame('select * from player;', connection)
connection.close()
df = pd.merge(
pd.merge(team, coach, on='teamID'),
player, on='teamID')
The DataFrame now looks like this:
In [2]: df
Out[2]:
teamID teamName coachID coachName playerID playerName
0 1 Red 1 Rachel Evans 1 Carol Lee
1 1 Red 1 Rachel Evans 2 Abigail O'Neil
2 1 Red 1 Rachel Evans 3 Becky Hood
3 1 Red 1 Rachel Evans 4 Bridget Sawyer
4 1 Red 2 Gladys Nenn 1 Carol Lee
5 1 Red 2 Gladys Nenn 2 Abigail O'Neil
6 1 Red 2 Gladys Nenn 3 Becky Hood
7 1 Red 2 Gladys Nenn 4 Bridget Sawyer
8 2 Green 3 Reina Stevens 5 Amy Reid
9 2 Green 3 Reina Stevens 6 Angie Costa
10 2 Green 3 Reina Stevens 7 Annie Reese
11 2 Green 3 Reina Stevens 8 Barbara Lo
12 2 Green 4 Jill Hunt 5 Amy Reid
13 2 Green 4 Jill Hunt 6 Angie Costa
14 2 Green 4 Jill Hunt 7 Annie Reese
15 2 Green 4 Jill Hunt 8 Barbara Lo
16 3 Blue 5 Lynn Peters 9 Alicia Green
17 3 Blue 5 Lynn Peters 10 Beth Spire
18 3 Blue 5 Lynn Peters 11 Candace Pierce
19 3 Blue 5 Lynn Peters 12 Carmen Jones
20 3 Blue 6 Stephanie Lenter 9 Alicia Green
21 3 Blue 6 Stephanie Lenter 10 Beth Spire
22 3 Blue 6 Stephanie Lenter 11 Candace Pierce
23 3 Blue 6 Stephanie Lenter 12 Carmen Jones
And now I would like to create a MultiIndex that shapes this data so that it looks something like this:
In [2]: df
Out[2]:
teamID teamName coachID coachName playerID playerName
1 Red 1 Rachel Evans 1 Carol Lee
2 Gladys Nenn 2 Abigail O'Neil
3 Becky Hood
4 Bridget Sawyer
2 Green 3 Reina Stevens 5 Amy Reid
4 Jill Hunt 6 Angie Costa
7 Annie Reese
8 Barbara Lo
I’ve been able to do this with straight Python, but I want to be able to take advantage of Pandas powerful and concise indexing features.
Adding the following
df.set_index(['teamID', 'teamName', 'coachID', 'coachName', 'playerID'], inplace=True)
makes the first four columns hierarchical. But the last two columns are still duplicated:
playerName
teamID teamName coachID coachName playerID
1 Red 1 Rachel Evans 1 Carol Lee
2 Abigail O'Neil
3 Becky Hood
4 Bridget Sawyer
2 Gladys Nenn 1 Carol Lee
2 Abigail O'Neil
3 Becky Hood
4 Bridget Sawyer
2 Green 3 Reina Stevens 5 Amy Reid
6 Angie Costa
7 Annie Reese
8 Barbara Lo
4 Jill Hunt 5 Amy Reid
6 Angie Costa
7 Annie Reese
8 Barbara Lo
Can someone show me the easiest way to normalize this table? I'm having trouble figuring out how to group relationships.
Film# Title Dir# DirectorName Actor# ActorName Role Timeon
F1100 Happy Day D101 Jim Alan A1000 Judy Foster Amy Jones 50
A1001 Robert Dinero Harry 30
A1004 Harrison Ford Jack Actress 55
A1009 Eddy Murphy Excel Fuse 65
A1000 Judy Foster Amy Jones 15
F1101 AirForceOne D105 Woody Ellen A1000 Judy Foster Amy Jones 50
A1004 Harrison Ford Bill Ford 55
A1009 Eddy Murphy Police one 35
A1002 Mary Jackson Police two 45
F1102 RunAwayBride D101 Jim Alan A1003 Julia Roberts Amy Jones 50
A1007 David Lynch Sam Strickland 45
A1004 Harrison Ford Robber 35
A1005 John Wayne Cab Driver 25
A1000 Judy Foster News Reporter 65
You need two data tables, Film and Person, and two intersection tables, FilmDirector and FilmRole. An abbreviated example:
Film
filmID title
1100 Happy Day
1101 Airforce One
1102 Runaway Bride
Person
personID name
101 Jim Alan
105 Woody Ellen
1000 Judy Foster
1001 Robert Dinero
1002 Mary Jackson
1003 Julia Roberts
1004 Harrison Ford
1005 John Wayne
1007 David Lynch
1009 Eddy Murphy
FilmDirector
filmDirectorID filmID personID
1 1100 101
2 1101 105
3 1102 101
FilmRole
filmRoleID FilmID personID role timeOn
1 1100 1000 Amy Jones 50
2 1100 1001 Harry 30
3 1100 1004 Jack Actress 55
6 1101 1000 Amy Jones 50
7 1101 1004 Bill Ford 55
...