I want to join 4 database tables together. Each of the tables have have the studenId field in common and all the other fields are different.
I want to have something like this
studentId|beginTime|endTime|audioText|sentiment|bpm|long|lat|movementcoordinates|
These fields should be filled in on the basis of the values from the other table.
This is the code I have tried
USE signals_db;
INSERT INTO trainingTable
SELECT audio.beginTime, audio.endTime, audio.audioText, audio.sentiment,
heartrate.bpm, locations.long, locations.lat,
movements.gravityX, movements.gravityY, movements.gravityZ,
movements.accX, movements.accY, movements.accZ, movements.rotX,
movements.rotY, movements.rotZ,
movements.attRoll, movements.attPitch, movements.attYaw, movements.fallenDown
FROM audio
INNER JOIN heartrate
ON audio.studentId = heartrate.studentId
INNER JOIN locations
ON audio.studentId = locations.studentId
INNER JOIN movements
ON audio.studentId = movements.studentId
With the above code a lot of duplicate values will be created in the joined table.
And I tried several answers already but most of things do not work.
I hope you can help me out.
Here is the picture of the joined table:
If really you obtain duplicated rows
You could use DISTINCT for obtain on a single rows for each duplciated
SELECT DISTINCT audio.beginTime, audio.endTime, audio.audioText, audio.sentiment,
heartrate.bpm, locations.long, locations.lat,
movements.gravityX, movements.gravityY, movements.gravityZ,
movements.accX, movements.accY, movements.accZ, movements.rotX,
movements.rotY, movements.rotZ,
movements.attRoll, movements.attPitch, movements.attYaw, movements.fallenDown
FROM audio
INNER JOIN heartrate
ON audio.studentId = heartrate.studentId
INNER JOIN locations
ON audio.studentId = locations.studentId
INNER JOIN movements
ON audio.studentId = movements.studentId
Related
DB MODEL
I have Model Which is Connected in above Order Now I want to Select Style Names From Style Table.
Case 1 : I want the Style Names which are both intersecting Category and Gender
Case 2 : I want the Style Names which are intersecting Gender only regardless of category.
I am newbie to SQL is there any efficient way to Get the desired result using SQL JOINS . Any Help in such case will be appreciated or is there any modelling solutions to deal with such type of situations.
Here is how you would join your tables:
SELECT *
FROM `style`
INNER JOIN `StyleCategoryGender` ON `StyleCategoryGender`.`SID` = `style`.`Code`
INNER JOIN `StyleCategoryGender` ON `Category`.`Code` = `StyleCategoryGender`.`GID`
INNER JOIN `StyleCategoryGender` ON `Gender`.`Code` = `StyleCategoryGender`.`CID`
then you can add WHERE clauses as needed
SELECT *
FROM `style`
INNER JOIN `StyleCategoryGender` ON `StyleCategoryGender`.`SID` = `style`.`CODE`
INNER JOIN `StyleCategoryGender` ON `Category`.`Code` = `StyleCategoryGender`.`GID`
INNER JOIN `StyleCategoryGender` ON `Gender`.`Code` = `StyleCategoryGender`.`CID`
WHERE `Gender`.`Name` = 'Female'
I'm using this query on mysql:
SELECT
`sitio`.`id_unico_sitio` AS 'ID Unico',
`sitio`.`nombre` AS 'Nombre',
`departamentos`.`departamento` AS 'Departamento',
`municipios`.`municipio` AS 'Municipio',
`sitio`.`direccion` AS 'Direccion',
`sitio`.`coor_x` AS 'Latitud',
`sitio`.`coor_y` AS 'Longitud',
`operador`.`nombre` AS 'Operador',
`tipo_tec`.`tecnologia` AS 'Tecnologia',
`sitio`.`ancho_banda` AS 'Ancho Banda(Mb/s)',
`sitio`.`fecha_inst` AS 'Fecha Instalacion',
`sitio`.`cod_sace` AS 'Cod SACE',
`sitio`.`cod_cnt` AS 'Cod CONATEL'
FROM ((((((((`sitio`
INNER JOIN `tipo_tec_sitio`
ON `sitio`.`id_unico_sitio` = `tipo_tec_sitio`.`id_unico_sitio`)
INNER JOIN `tipo_tec`
ON `tipo_tec_sitio`.`id_tipo_tec` = `tipo_tec`.`id_tipo_tec`)
INNER JOIN `administracion`
ON `sitio`.`id_administracion` = `administracion`.`id_administracion`)
INNER JOIN `estatus_finalizado`
ON `sitio`.`id_estatus_finalizado` = `estatus_finalizado`.`id_estatus_finalizado`)
INNER JOIN `departamentos`
ON `sitio`.`id_departamento` = `departamentos`.`id_departamento`)
INNER JOIN `municipios`
ON `sitio`.`id_municipio` = `municipios`.`id_municipio`)
INNER JOIN `sitios_has_operador`
ON `sitio`.`id_unico_sitio` = `sitios_has_operador`.`id_unico_sitio`)
INNER JOIN `operador`
ON `sitios_has_operador`.`id_operador` = `operador`.`id_operador`)
The problem is that with that query I'm only getting the first 2 rows when on the database there are five rows.
Is there something wrong or something that limits the number of rows returned from the database on that query?
Since you are using INNER JOIN for all the tables, are you sure you have all five rows inside all the tables ? if one of the table only contains two rows, then at the end it will only show two rows.
Make sure that your INNER JOIN condition matches all the five rows in the table you need.
Those 2 are my tables I use for my data. Now when I want to join those two tables I stuck at JOIN ON golub...
I know I'm making my mistake there but I don't know what is it. Values beneath IDmajka and IDotac sometimes may be 0. That value 0 is from table "golub" and it doesn't exist. Even If I put values that exists in table "golub" it still doesn't work. It won't collect any data.
Please ignore JOIN on drzava and status cause it works.
my query
SELECT * FROM popis_golubova
JOIN golub ON (golub.ID = popis_golubova.IDgolub
AND golub.ID = popis_golubova.IDmajka
AND golub.ID = popis_golubova.IDotac)
JOIN drzava ON (drzava.ID=popis_golubova.IDdrzava)
JOIN status ON (status.ID=popis_golubova.IDstatus)
WHERE popis_golubova.IDkorisnik='$ID_KORISNIK'
table "golub"
table "popis_golubova"
This is solution if it is going to help someone
SELECT
O.brojgoluba AS o_brojgoluba,
M.brojgoluba AS m_brojgoluba,
golub.spol, golub.boja, golub.rasa, golub.ime, golub.godina, golub.brojgoluba, drzava.drzava, status.status
FROM popis_golubova
JOIN drzava ON (drzava.ID=popis_golubova.IDdrzava)
JOIN status ON (status.ID=popis_golubova.IDstatus)
JOIN golub AS O ON (O.ID=popis_golubova.IDotac)
JOIN golub AS M ON (M.ID=popis_golubova.IDmajka)
JOIN golub ON (golub.ID=popis_golubova.IDgolub)
WHERE popis_golubova.IDkorisnik='$ID_KORISNIK'
ORDER BY popis_golubova.IDgolub
I have a problem with joining some tables, heres my structure:
tbl_imdb:
fldID fldTitle fldImdbID
1 Moviename 0000001
tbl_genres:
fldID fldGenre
1 Action
2 Drama
tbl_genres_rel:
fldID fldMovieID fldGenreID
1 1 1
2 1 2
What I’m trying to do is a query that will find all movies that is both an action movie and drama, is this possible to do without a subquery, if so, how?
What I'm trying right now is:
SELECT tbl_imdb.*
FROM tbl_imdb
LEFT JOIN tbl_imdb_genres_rel ON ( tbl_imdb.fldID = tbl_imdb_genres_rel.fldMovieID )
LEFT JOIN tbl_imdb_genres ON ( tbl_imdb_genres_rel.fldGenreID = tbl_imdb_genres.fldID )
WHERE tbl_imdb_genres.fldGenre = 'Drama'
AND tbl_imdb_genres.fldGenre = 'Action';
But this dosnt work, however it does work if I only keep one of the two WHERE's, but thats not what I want.
Two ways to do it:
1
SELECT tbl_imdb.*
FROM tbl_imdb
INNER JOIN tbl_genres_rel rel_action
ON tbl_imdb.fldID = rel_action.fldMovieID
INNER JOIN tbl_genres genre_action
ON rel_action.fldGenreId = genre_action.fldID
AND 'Action' = genre_action.fldGenre
INNER JOIN tbl_genres_rel rel_drama
ON tbl_imdb.fldID = rel_drama.fldMovieID
INNER JOIN tbl_genres genre_drama
ON rel_drama.fldGenreId = genre_drama.fldID
AND 'Drama' = genre_drama.fldGenre
This method is on the same path as your original solution. 2 differences:
The join should be inner, not left because you're trying to get movies that certainly have the corresponding genre entry
Since you want to find 2 different generes, you'll have to do the join with tbl_genres_rel and tbl_genres twice, once for each particular genre you're interested in.
2
SELECT tbl_imdb.*
FROM tbl_imdb
INNER JOIN tbl_genres_rel
ON tbl_imdb.fldID = tbl_genres_rel.fldMovieID
INNER JOIN tbl_genres
ON tbl_genres_rel.fldGenreId = tbl_genres.fldID
AND tbl_genres.fldGenre IN ('Action', 'Drama')
GROUP BY tbl_imdb.fldID
HAVING COUNT(*) = 2
Again, the basic join plan is the same. Difference here is that we join to the tbl_genres_rel and tbl_genres path just once. This on itself fetches all genres for one film, and then filters for the one's you're interested in. The ones that qualify will now have 2 rows for each distinct value of tbl_imdb.fldId. The GROUP BY aggregates on that, flattening that into one row. By asserting in the HAVING clause that we have exactly 2 rows, we ensure that we keep only those rows that have both the genres.
(Note that this assumes that there is a unique constraint on tbl_genres_rel over {fldMovieID, fldGenreID}. If such a constraint is not present, you should consider adding it.)
LEFT JOIN is not applicable in your case because records should exist on both tables. And you need to count the instances of the movie
SELECT *
FROM tbl_imdb a
INNER JOIN tbl_genres_rel b
on a.fldID = fldMovieID
INNER JOIN tbl_genres c
on c.fldGenreID = b.fldID
WHERE c.fldGenre IN ('Drama', 'Action')
GROUP BY a.Moviename
HAVING COUNT(*) > 1
I have this query with many left joins and a inner join with dates.
I need to group by id_art (from articles_art) and date_dat (dates_dat). The problem is that is really slow. it takes 3second for 1000records.
dates_dat is indexed in dates_dat table and id_art is a primary key of articles_art.
What can I do to optimize this query?
SELECT
id_art, image2_art, video_art, website,
text.title_int, text.intro_int, text.text_int, text.extra_int,
dat.date_dat, dat.date2_dat,
group_concat(tim.time_tim),
prd.name_prd,
group_concat(cat.name_cat),
trg.name_trg,
spa.name_spa,
spa2.name_spa
FROM
articles_art AS art
LEFT JOIN internText_int AS text ON text.idart_int = art.id_art
INNER JOIN dates_dat AS dat ON art.id_art = dat.idart_dat
LEFT JOIN spaces_spa As spa ON spa.id_spa = dat.idspa_dat
LEFT JOIN spaces_spa As spa2 ON spa.id_spa = dat.idspa2_dat
LEFT JOIN times_tim AS tim ON tim.iddat_tim = dat.id_dat
LEFT JOIN articles_products_artprd AS artprd ON artprd.idart_artprd = art.id_art
LEFT JOIN products_prd AS prd ON prd.id_prd = artprd.idprd_artprd
LEFT JOIN cater_cev AS cev ON cev.idart_cev = dat.idart_dat
LEFT JOIN categories_cat AS cat ON cat.id_cat = cev.idcat_cev
LEFT JOIN targets_trg AS trg ON trg.id_trg = art.idtrg_art
WHERE
prd.id_prd in (1,2)
AND validated_art = 1
AND text.idlin_int in (1,4)
GROUP BY
id_art, date_dat
Look like you can put an index on these columns
prd.id_prd
validated_art
text.idlin_int
Test this first then if this does not work put indexes on column conditions on the ON clause
If data latency isn't an issue, can you hive the data off (perhaps overnight?) into a single normalised table? That way you query a single table without all those JOINS. You could even apply indexes to help speed things up further.