Select from 2 tables - ms-access

I have 2 tables , First table is named Ticket
Ticket
ID
Subject
Owner
second table is
TicketLinkedNames
ID
TicketID
ContactID
LinkedReason
from the above structure you understand that I can link the table ticket with some other names so when I entered to the ticket form I can see in a grid below all the link names that are associated with this ticket. Am ok here?
My main problem is that I have one main form that I want to display all the tickets that the owner is some contact and also all the tickets that this contact is appear as link name in other tickets..
sort them out by unique records and display them.
I am really confused of what kind of select query should I use, I have tried several like:
Select * from Ticket,TicketLinkedNames where Owner=ContactID
but returns wrong records. I have use inner join between ID=TicketID but also return wrong records.
I am really confused, please if someone could help me i will really appreciated.
I am using Microsoft Access 2007.

make 2 selects
SUB select1:
select distinct from table Ticket
for the owner that not contained in TicketLinkedNames
SUB Select2:
select distinct from table TicketLinkedNames
for the owner
then make a union between the 2
then you got the solution

Related

MySQL 2 queries combine into 1 query - self referencing

We have build a very simple referral system that tracks userID's without cookies and referrals for social media. I am trying to create something like a 'leader board' so I can show the UserID of the top leaders in the database.
I'm trying to combine these 2 queries into 1 query.
SELECT
Count(users.AffiliateID) AS affiliate,
users.AffiliateID
FROM
users
group by affiliateID
order by affiliate desc
This generates an output where the variable 'affiliate' contains the USERID of the top referring affiliate. In this case the #1 person is affiliateID = 5297dc41331235
What I then do is look up the name of the person with this query.
Select name from users where UserIDHash = "5297dc41331235";
How can I rewrite the query above so that it looks up the value of the name field as a column that references the value of the AffiliateID i.e. where AffiliateID=UserIDHash on each record?
Your help is greatly appreciated.
Thanks!

How do I join all rows with the same name in one table using MYSQL?

Suppose I have a database called clubmembership that has a column for names, a column for clubs, and a column for the role they play in that club. The name Margrit would be in the column name many times, or as many times as she is in a club. If I want to see which people are members of the sewing club my query might look something like this:
SELECT DISTINCT NAME FROM CLUBMEMBERSHIP
WHERE CLUB=’SEWING’
AND ROLE=’MEMBER’;
My problem is that I can't figure out a query for who is not in the sewing club. Of course the simple 'not in' clause isn't working because there are plenty of rows which sewing does not appear in. In this database if someone is not in the sewing club, sewing does not appear under club so I imagine there is a way to join the different rows with the same name under 'name' and then potentially use the 'not in' clause
I hope this was a good explanation of this question. I have been struggling with this problem for a while now.
Thanks for your help!
Nicolle
This is not something that can be solved by just changing the existing code, it is to do with the database design.
Database normalisation is the process of sorting out your database into sensible tables.
If you’re adding a person many times, then you should create a table called members instead. And if there is a list of clubs, then you should create a clubs table.
Then, you can create a table to join them together.
Here’s your three tables:
members
-------
id (int)
name (varchar)
clubs
-------
id (int)
name (varchar)
memberships
-------
member_id (int)
club_id (int)
Then you can use joins in MySQL to return the information you need.
Stack Overflow doesn’t like external links as the answer should be here, but this is a huge topic that won’t fit in a single reply, so I would briefly read about database normalization, and then read about ‘joining’ tables.
If I understand you correctly, you wanted to list all names that is not a member of SEWING. The Inner query will get all Names that are member of SEWING, however, the NOT EXISTS operator will get all Names that are not found in the inner query.
SELECT DISTINCT C.NAME
FROM CLUBMEMBERSHIP C
WHERE C.ROLE = 'MEMBER'
NOT EXISTS
(
SELECT NULL
FROM CLUBMEMBERSHIP D
WHERE D.CLUB='SEWING'
AND D.ROLE='MEMBER'
AND C.NAME = D.NAME
)
Here's a Demo.

MySQL list all rows in one table excluding the ones already referenced by another table

I've spent some time tearing my hair out and combing the internet for help, but I'm not having much luck. I feel like I'm missing something obvious and I'm tired and annoyed lol
I have two tables. One is called RolesAndServices and one is called Roles. They look like this.
Roles
- ID
- Name
RolesAndServices
- ID
- JobID
- RoleID
- ClientID
- PeopleID
What I am attempting to achieve is something like this:
SELECT Roles.ID, Roles.Name FROM Roles, RolesAndServices WHERE
RolesAndServices.RoleID = Roles.ID AND NOT RolesAndServices.JobID = 1
The reason that does not work is because all the items in RolesAndServices (currently) have the JobID 1. Fair enough. So I get why that doesn't work.
What I am attempting to achieve is a list of all the items in Roles, excluding those Roles that are both
Present in the RolesAndServices table and
Have a JobID that is set to 1
Basically the end goal is to create a HTML select that will contain options for every role except those already set up for that job.
For example, say there are three roles in total:
Teacher
Student
Parent
If a job has already had the teacher role added to it, RolesAndServices has an entry with the roleid of teacher and the jobid of the job in question. When adding another role to that job, the available options should be
Student
Parent
I've tried a variety of join based queries as well to no avail. Selecting all the roles and then excluding those that have been used in rolesandservices is achieveable, I just stumble when limiting the exclusion to a particular job number.
Thanks in advance for any help!
using NOT EXISTS
SELECT ID, Name
FROM Roles ro
WHERE NOT EXISTS
(
SELECT NULL
FROM RolesAndServices rs
WHERE ro.ID = rs.RoleID
AND rs.JobID = 1
)

Specific MYSQL view with null values

I'm having trouble creating a view for one of my MYSQL assignments. I understand how to create a view technically, as in, the commands to do so. (I have already done a few other different views for this assignment) My problem is with how to design this particular view... I don't know how to with the knowledge I have/The way I designed my tables.
So, I have 2 relevant tables(There are 2 others but I don't think they are needed for this problem): Attendance and Scholar. I need to create a view where all scholars are listed as well as the date where they were an invited speaker. However, if they were never an Invited Speaker, the date should have a null value shown. So I need to select First Name and LastName from Scholar and ADate from Attendance. Attendance has the column AttendanceType that can be either Invited Speaker or Chairman. Attendance also has the foreign key LName, relating to LastName, and ADate obviously. I can't conceptually think of have to do this, I thought that using a join, which I'm not that experienced with would be the right choice but it didn't work...
Here's what I attempted
CREATE VIEW InvitedScholars
AS SELECT FirstName,LastName,ADate
FROM Scholar LEFT JOIN Attendance ON AttendanceType='Invited Speaker'
WHERE Lname=LastName;
This only gave me Invited Speakers, not all Scholars... I don't know how to progress... any advice would be appreciated.
You need to do your left join on the Last Name (assuming this is your key on both tables). See the SQL below:
CREATE VIEW InvitedScholars
AS SELECT FirstName,LastName,ADate
FROM Scholar LEFT JOIN Attendance ON Scholar.LastName = Attendance.LName
AND Attendance.AttendanceType = 'Invited Speaker';
It appears you have your join and where clauses mixed up. You want to join the the two tables on the last name (which invites another issue if you have two speakers with the same last name) and filter by AttendanceType
FROM Scholar LEFT JOIN Attendance ON Lname=LastName
WHERE AttendanceType='Invited Speaker'

Access 2010 DLookUp

Working with MS Access for the first time and coming across a few problems if someone could please point me in the right direction.
So I'm doing a mock database (so it looks silly) just to learn the ins and outs and need some help with DLookUp at the moment.
My database has two tables, with the following fields:
C_ID the PK in Courses and FK in Student
tblCourse: C_ID, Title, Subject
tblStudent: S_ID, C_ID, Name, EnrollDATE
As I said this is just for testing/learning. So what I want is to have a filter that gives me a list of C_ID's based on which EnrollDates are NULL.
so filter is:
Expr1: DLookUp("[tblStudent]![C_ID]","tblStudent","isNull([tblStudent]![EnrollDATE])")
I have also tried with the criteria being
[tblStudent]![EnrollDATE] = Null
Currently I get just blank fields returned. Any help is greatly appreciated, and please ask me to elaborate if my explanation is off.
Thank You!
The correct syntax looks like this:
DLookup("C_ID", "tblStudent", "EnrollDate is null")
You don't need to include the table name when you specify the columns
In Access, you check for Null by using xxx is null or xxx is not null
Note that DLookup only returns one value (if the criteria matches more than one row, the value is taken from any row), so you can't use it to get a list of C_IDs back.
EDIT:
What you actually want to do is select data from one table, and filter that based on data from the other table, correct?
Like, selecting all courses where at least one student has an empty EnrollDATE?
If yes, you don't need the DLookup at all, there are two different ways how to do that:
1) With a sub-select:
select *
from tblCourse
where C_ID in
(
select C_ID
from tblStudents
where EnrollDATE is null
)
2) By joining the tables:
select tblCourse.*
from tblCourse
inner join tblStudent on tblCourse.C_ID = tblStudent.C_ID
where tblStudent.EnrollDATE is null
This is SQL, so you need to switch to SQL View in your query in Access.