Join data from multiple columns and 2 tables - mysql

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

Related

MySql - having issues with double left join

I am having issues with getting this double left join to get the listingspecificsListPrice, but that info exists in the table, cant figure out why it would not include it. This is my sql.
SELECT mls_subject_property.*, mls_images.imagePath, mls_forms_listing_specifics.listingspecificsListPrice
FROM mls_subject_property
LEFT JOIN mls_images ON mls_subject_property.mls_listingID = mls_images.mls_listingID
LEFT JOIN mls_forms_listing_specifics ON mls_forms_listing_specifics.mls_listingID = mls_subject_property.mls_listingID AND mls_images.imgOrder = 0
WHERE userID = 413
GROUP BY mls_subject_property.mls_listingID
The result comes out like this..
All of the other fields come back, but it doesnt seem to want to bring back those two items.
This is a picture of the other table, to show that the data does in fact exist.
The mls_images.imgOrder = 0 condition should be in the join with mls_images, not mls_forms_listing_specifics.
Don't use GROUP BY if you're not using any aggregation functions. Use SELECT DISTINCT to prevent duplicates.
SELECT DISTINCT mls_subject_property.*, mls_images.imagePath, mls_forms_listing_specifics.listingspecificsListPrice
FROM mls_subject_property
LEFT JOIN mls_images ON mls_subject_property.mls_listingID = mls_images.mls_listingID AND mls_images.imgOrder = 0
LEFT JOIN mls_forms_listing_specifics ON mls_forms_listing_specifics.mls_listingID = mls_subject_property.mls_listingID
WHERE userID = 413

Joining 4 tables together mysql (getting many duplicate values)

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

Can we write condition in ON clause of left join which may or may not satisfy?

I have a table for audit trail which saves all actions performed records through out the project like add update and delete.
there I maintain a column which saves primary keys of multiple tables on which action is performed. This is integer column
my query is like this
select * from
user usr1
left join activity_history
on activity_history.userID= usr1.sequenceID
left join candidate can1 on can1.userID = usr1.sequenceID
and can1.userID = activity_history.activity_sequenceID
left join institute ins1 on ins1.userID= usr1.sequenceID
and ins1.userID = activity_history.activity_sequenceID
left join candidate_institutes caninst on caninst.candidateID = can1.candidateID and caninst.instituteID= ins1.instituteID
left join exam exam1 on exam1.instituteID = ins1.instituteID
and exam1.examID = activity_history.activity_sequenceID
left join proctor pro1 on pro1.userID = usr1.sequenceID
and pro1.proctorID = activity_history.activity_sequenceID
left join appointment appt1 on appt1.examID = exam1.examID
and appt1.sequenceID = activity_history.activity_sequenceID
/* COMMENTED CODE-----
on( activity_history.activity_sequenceID=can1.userID
OR activity_history.activity_sequenceID=ins1.userID
OR activity_history.activity_sequenceID=exam1.examID and activity_history.userID= usr1.sequenceID
OR activity_history.activity_sequenceID=pro1.userID
OR activity_history.activity_sequenceID=appt1.sequenceID
)
*/
order by activity_history.sequenceID desc
activity_sequenceID is the column where I am storing keys of other tables
I need to map that. Is this a right way to join these tables or Commented part could be the rite way ?
Or is there any other way to join these tables.
I am confused because I am writing a condition in but activity_histry table may or may not have records of that particular table .

Double query a table from two fields (INNER JOIN?)

I hope I can explain myself.
I have a many to many table (asignaciones) which points to alumnos and invest tables. Both of those tables has a institucionID which points to instituciones table.
I need to get (in one query) both instituciones from alumnos and invest. I have this but is not complete. I guess if because of the AND in the last inner join:
SELECT
alumnos.alumnosID,
invest.investigadoresID,
asignaciones.alumnosID AS alumnosID1,
asignaciones.investigadoresID AS investigadoresID1,
instituciones.institucion
FROM alumnos
INNER JOIN asignaciones ON alumnos.alumnosID = asignaciones.alumnosID
INNER JOIN invest ON asignaciones.investigadoresID = invest.investigadoresID
INNER JOIN instituciones ON alumnos.institucionesID = instituciones.institucionesID AND invest.institucionesID = instituciones.institucionesID
This lacks the second institucion. I am getting just one
Any hints on this is really appreciated
This Query schold solve your problem:
SELECT
alumnos.alumnosID,
invest.investigadoresID,
asignaciones.alumnosID AS alumnosID1,
asignaciones.investigadoresID AS investigadoresID1,
instituciones.institucion
instituciones1.institucion
FROM alumnos
INNER JOIN asignaciones ON alumnos.alumnosID = asignaciones.alumnosID
INNER JOIN invest ON asignaciones.investigadoresID = invest.investigadoresID
INNER JOIN instituciones instituciones ON alumnos.institucionesID = instituciones.institucionesID
INNER JOIN instituciones instituciones1 ON invest.institucionesID = instituciones1.institucionesID
You have to join the last table twice.

mysql: fast group by with join

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.