SQLite obtaining client data from same table based on partnerID - sqlite

I've spent hours searching for an answer on this and cannot find a solution.
I have single table with client data. Each client has a unique client id, and also has a partner id if they have a partner. The partner would also have their own row with their unique client id and their partners id.
This is how a basic version of the table called Clients would look:
ClientID
PartnerID
ClientName
001
002
John Doe
002
001
Jane Doe
I want to write a query that searches for John Doe, and then obtains data about Jane Doe based on the PartnerID when querying John Doe.
The basics I have currently are just querying John Doe
SELECT
ClientName,
PartnerID
FROM
Clients
WHERE Clients.ClientName = 'John Doe'
This of course displays the data for John Doe:
ClientID
PartnerID
ClientName
001
002
John Doe
but how can I use the PartnerID from this query to also show me the records for Jane Doe?

You can use a subquery for that:
SELECT ClientName, PartnerID
FROM Clients
WHERE ClientID IN (
SELECT PartnerID
FROM clients
WHERE Clients.ClientName = 'John Doe')
or you can join the table to itself
SELECT partner.clientName, partner.partnertID
FROM clients
JOIN clients AS partner ON clients.PartnerID = partner.ClientID
WHERE clients.ClientName = 'John Doe'
Both these methods are very common in sql, I suggest you read the sql tutorial in the postresql documentation which is a great starting point in SQL.
https://www.postgresql.org/docs/15/tutorial.html

Related

search function can not find user by name and surname

When I search for a user by Name and Surname, for example, John Doe, I can't find the user. Even there is a user by that name and surname in DB. However, when I search just for NAME or Surname I will find the user. How can I fix this?
public List<User> SearchByName(string search)
{
return _dBcontext.Users.Where(x => x.Name.Contains(search) || x.Surname.Contains(search)).ToList();
}
When searching for "John Doe", your query is searching for records whose Name or Surname contain the entirety of "John Doe", e.g. "John".Contains("John Doe"). There are a few ways to resolve this, the two simplest that I can think of being:
Split apart your search input into firstName and surname variables, and compare the variables to their respective record variables,
use search.Contains(x.Name) || search.Contains(x.Surname)

Querying DynamoDB with partition key and secondary index in Javascript

Having the following table definition in DynamoDB
pk: userId (string)
sk: carId (string)
index: carModelIndex (string)
If I would like to query all the cars rented by a given user for a given model. How I would go about that?.
Please note I am using the javascript sdk for dynamodb.

Attendance System MS ACCESS

I have 2 tables :
StudentInfo
Attendance
The StudentInfo table has fields about students like: RollNo, FirstName, LastName etc...
The Attendance table has the fields: RollNo, FirstName, LastName, InClass(Yes/No) and Date.
I want to duplicate the common fields from StudentInfo to Attendance and when I add a new entry in the StudentInfo table it will automatically add them to the Attendance table.
Also, when I add attendances in the Attendance table then I want to be able to view the data by months or dates, so that I can see on what day how many students attend the class.
Can you suggest a way for me to store attendance of students like we store on register, your help will be really appreciated!

Firebase getting data from database

so my database is as follows
Program1
Posts:
PostsID1:
Name: John
Age: 12
User_Id: nv4h8fh4uen8h8f4ufn
PostsID2
Name: Don
......
I have the user Id for the user, how can i check if that user has post in the database and if he does just return his post rather than all the posts.
For example // nv4h8fh4uen8h8f4ufn is user id.. he has a post so it will return John, 12,
I dont know if I understood right but, the most common is use the users id as a key, and with that generate child keys for his posts, like this:
Users:
nv4h8fh4uen8h8f4ufn1:
Name: John
Age: 12
Posts:
idPost1:
postData
idPost2:
postData

Insert data where Multiple Foreign keys are in SQL server table

I have three Tables:
Teachers
PK: A_pk
Students
PK: B_pk
Post
PK: C_pk
FK: A_pk
FK: B_pk
I have a Website page where users write different posts.
When teachers post in that group, I will insert data into the Post table like this:
A_pk = teacherName
C_pk = Post_text
When students post in that group, I will insert data into the Post table like this:
B_pk = studentName
C_pk = Post_text.
Reason is that I want to keep record which user posted data in my group.
Now the Question is how to insert record in Post table?
You cannot have one field in your table be a foreign key to two different tables. What you need to do is change your data structure to either hold your students and teachers in the same table and use that primary key in your Post table, or to have two columns in your Post table - one for the Teacher primary key and one for the Student primary key and populate the appropriate one based on who made the post.
There is absolutely no need nor reason to have the ID of two different tables within the same field. Doing so would be bad design.
Finally--- I found the Solution.
Actually its very simple... Like you can have nullable FK.so when you insert for student then A_PK can be null and vice versa. –
If teacher posted data in group than
if(A_pk != null) { Insert A_pk and Insert C_pk }
If student posted data in group than
else if(B_pk != null) { Insert B_pk and Insert C_pk }
Thanks alot Sir
KumarHarsh 1 hour ago

Resources