i have three tables like follows:
this one table : m_application_resources
resourceid resource_name menu_group_id menu_name creation_date last_created_by updation_date last_updated_by
and the second table : m_roles
roleid rolename description creation_date last_created_by updation_date last_updated_by
and the third table is : m_access_matrix
accessid resourceid roleid creation_date last_created_by updation_date last_updated_by
Relationship for the table is : resourceid and roleid
and this my query
select am.accessid, ar.resource_name,rls.rolename
from m_application_resources ar, m_roles rls,m_access_matrix am
where ar.resourceid=am.resourceid
this returns the following :
ccessid resource_name rolename
1 DepartmentAction Admin
1 DepartmentAction Client
1 DepartmentAction Doctors
2 PositionsAction Admin
2 PositionsAction Client
2 PositionsAction Doctor
the result is wrong and i don't know how to go about.
Ex : DepartmentAction should come only once and the role name should be any one.
SqlfiddleFiddle
Please help
You do not have a condition on role_id:
select am.accessid, ar.resource_name,rls.rolename
from m_application_resources ar, m_roles rls,m_access_matrix am
where ar.resourceid=am.resourceid AND am.roleid = rls.roleid
I would convert the query to ANSI SQL syntax for joins for better clarity:
SELECT am.accessid, ar.resource_name,rls.rolename
FROM m_application_resources ar
JOIN m_access_matrix am ON ar.resourceid=am.resourceid
JOIN m_roles rls ON am.roleid = rls.roleid
It would be best to explicitly join the tables in your query. I'm not 100% sure I understand your tables, but this should work:
SELECT am.accessid, ar.resource_name, rls.rolename
FROM (m_application_resources ar INNER JOIN m_access_matrix am
ON ar.resourceid = am.resourceid) INNER JOIN mroles rls ON am.roleid = rls.roleid
Related
I have these three tables:
The table "Clienti" contains the customers.
The table "Corsi" contains all the available courses
The table "corsi Fatti" contains all the Courses each client has taken.
what I would need is a query that returns each client, and what courses he attended on what date.
For that I would like to have for example a table returned with these columns:
Clienti.Nome, corsi.row1.corso, corsi.row2.corso, corsi.row3.corso,corsi.rowN.corso.
and the content of the table should be:
clienti.Nome, corsifatti.data of the matching course in the corsi table if present.
so, first column is the client name, and then there is a column for each row of the "corsi" table, and if a client has partecipated on that course then the corsifatti.data should be in that column.
Can something like this be done with a Access or Mysql Query? I have tried with inner joins but the result was not what I need.
select
Clienti.nome, Clienti.Addresse, Clienti.CAP, Clienti.Tel,
Clienti.Ansprechpartner, Clienti.Mail, Clienti.Weiteres,
CorsiFatti.Data, Corsi.Corso, Corsi.Durata
from Clienti
INNER JOIN CorsiFatti on CorsiFatti.[ID Cliente] = Clienti.ID
INNER JOIN Corsi on Corsi.ID = CorsiFatti.[ID Corso]
What you are asking is a simple inner join:
select Clienti.nome, Clienti.Addresse, Clienti.CAP,
Clienti.Tel, Clienti.Ansprechpartner, Clienti.Mail,
Clienti.Weiteres,
CorsiFatti.Data,
Corsi.Corso, Corsi.Durata
from Clienti
INNER JOIN CorsiFatti on CorsiFatti.[ID Cliente] = Clienti.ID
INNER JOIN Corsi on Corsi.ID = CorsiFatti.[ID Corso]
order by Clienti.nome, Corsi.Corso;
I think you meant yours was lacking the order by only.
I wouldn't suggest using access, but if it is access anyway, then you need to have parenthseses around all those joins (peculiar I know, but it is access).
The pivot with variable columns count is complicated. I can advice simplest solution uses JSON aggregation:
SELECT
ClienteId,
Name,
JSON_ARRAYAGG(
JSON_OBJECT("Data", Data, "Corso", Corso)
) Corsi
FROM
CorsiFatti
JOIN Corsi ON Corsi.Id = CorsiFatti.CorsoId
JOIN Clienti ON Clienti.Id = CorsiFatti.clienteId
GROUP BY
ClienteId,
Name
;
Result is row for each client contains client's data at his courses as JSON string:
+===========+=========+========================================================================================+
| ClienteId | Name | Corsi |
+===========+=========+========================================================================================+
| 1 | Mr. Fix | [{"Data": "2021-01-01", "Corso": "Corso1"}, {"Data": "2021-02-01", "Corso": "Corso3"}] |
+-----------+---------+----------------------------------------------------------------------------------------+
Here you can find SQL fiddle
i need help on this. I don't know how to use join in mysql so I try this to avoid to use join.
I have to have in the end this type of table:
tracce.titolo - data - aka (where tracce.idma=artist.id) - aka (for featuring. so i need to achive ida, that's id that identify the featured artist, and i do this under this condition feat.idt=tracce.id (so where the id from the track is the same for the id of the track in the feat table) and where artist.id=feat.ida (so where the id that identify the artist in feat table is the same of the one in the table of artists)).
I try this but it not work, that's the code:
SELECT tracce.titolo, tracce.data, artist.aka, feat.ida FROM tracce, artist, feat
WHERE tracce.idma=artist.id AND feat.idt=tracce.id
SELECT artist.aka FROM tracce
WHERE artist.id=feat.ida
Table structure is:
artist: id - aka
tracce: id - titolo - idma - data (id of track, titolo is title of the track, idma is the id of the main artist and data is date)
feat: idt - ida (idt is the id of the track and ida is the id of the featured artist)
EDIT: modify the code made by Christian, I solve adding another join on artist table using two different alias and it seems to work.
SELECT tracce.titolo, tracce.data, a1.aka as 'main', a2.aka as 'feat' from tracce
JOIN artist a1
ON tracce.idma = a1.id
JOIN feat
ON feat.idt = tracce.id
JOIN artist a2
ON a2.id = feat.ida
What you had in the first statement is an implicit join - not recommended as it will become obsolete. Not exactly sure if I understood, but perhaps this is what you're looking for?
SELECT tracce.titolo, tracce.data, artist.aka, feat.ida from tracce
JOIN artist
ON tracce.idma = artist.id
JOIN feat
ON feat.idt = tracce.id AND artist.id = feat.ida
I am using table aliases to make code shorter.
I know how to join two tables to check data, but I not sure how to join the third table. I have tried to find solution in the online, but I can't find solution it can match my problem. Hope someone can guide me how to join the third table. Thanks.
Below is my sql code, this sql code just to join two tables:
SELECT bl.id_book_name as bl_book_name
, bl.remark as bl_remark
, bl.status as status
, bl.return_date as bl_return_date
, anb.country as anb_country
, anb.title as book_name
, bl.date_borrowed as date_borrowed
FROM book_lending bl
JOIN add_new_book anb
ON bl.id_book_name = anb.id
My third table column in the below, table name is called user, I want to join the user name:
id |name |
This is my join two tables testing result:
Since you have not provided the full structure of USER table, I can only provide you the glimpse of joining third table. You may try below code -
SELECT bl.id_book_name as bl_book_name
, bl.remark as bl_remark
, bl.status as status
, bl.return_date as bl_return_date
, anb.country as anb_country
, anb.title as book_name
, bl.date_borrowed as date_borrowed
FROM book_lending bl
JOIN add_new_book anb ON bl.id_book_name = anb.id
JOIN user u ON u.id = <USER_ID column in 1 of above 2 tables>
I need to join twice the same table how do a do it in Codeigniter?
TABLE MATCH
match_id
team_home_id
team_away_id
score_home
score_away
group_id
round_id
winner
start_date
start_time
TABLE TEAM
team_id
name
code
This are the two tables I have to join. The table team has to join twice with a table match.
$this->db->select('*')
->from('match, team')
->join('team AS team_a', 'match.team_home_id = team_a.team_id')
->join('team AS team_b', 'match.team_away_id = team_b.team_id')
->get()->result();
My result is the match and just one of the teams :/
Thank you, Strawberry for your help.
On my query, I was getting always the last team joined because it looks like Codeigniter overwrites the columns with the same name, team_a.name and team_a.code are represented in the final result_array() as array key "name" and "code" the same as team_b.name and team_b.code.
So more than an alias for the tables I needed to add an alias for the columns:
$this->db->select('tips.stake, tips.odd,
matches.score_home, matches.score_away, matches.winner, matches.start_date, matches.start_time,
team_home.name AS team_home_name, team_home.code AS team_home_code, team_away.name AS team_away_name, team_away.code AS team_away_code');
$this->db->from('tips');
$this->db->join('matches', 'matches.match_id = tips.match_id');
$this->db->join('`teams` `team_home`', 'team_home.team_id = matches.team_home_id');
$this->db->join('`teams` `team_away`', 'team_away.team_id = matches.team_away_id');
$query = $this->db->get();
I have 2 tables : registered_students - cols: regnum AND student - cols: regnum, name
So I'm working on a query to select from both tables. It's working so far, but now I want to add a where clause to it, and it's not returning any rows. Here is the query
SELECT registered_students.regnum
, student.name
FROM registered_students
INNER JOIN student ON registered_students.regnum=student.regnum
example data:
registered_students 1).reg3030 2). reg4032
student 1).reg3030 John Doe 2).reg4032 Luke White
So I need to add a where clause like WHERE regnum LIKE 'reg4%'
You will have to specify the alias as the column has same name in both the tables, try the following query:
SELECT registered_students.regnum, student.name
FROM registered_students JOIN student ON registered_students.regnum=student.regnum
WHERE registered_students.regnum LIKE 'reg4%';
The output will also depend on the availability of data with reg4 in both the tables. You could try the following if the above does not work:
case insensitive matching, e.g. WHERE LOWER(registered_students.regnum) LIKE 'reg4%'
LEFT JOIN if you want the data from either of these tables
Update
With the updated data, it might be because of whitespace or a dot in the beginning of the value, I would try the following:
SELECT registered_students.regnum, student.name
FROM registered_students JOIN student ON TRIM(registered_students.regnum) = TRIM(student.regnum)
WHERE registered_students.regnum LIKE '%reg4%';