Search for column - "Name" instead of "Id" in foreign Table which represents the foreignId - laravel-8

I have a table named - mailbox in mailbox I have a foreignId which is company_id. In companies table I have columns like Id, Name.
Now, I want to make a search functionality where the search field will be used for searching by company name. Now, my problem is I have foreignId in companies table. But searching with one of that foreign table column which is not Id. If it was Id I could be able to figure it out.
In this case how can I achieve the objective?
This is my current code for search:
if ($request->has('search')){
$mails= Mailbox::with('companies')
->orWhere('from','LIKE', "%$request->search%")
->orWhere('title','LIKE', "%$request->search%")
->where('priority', $request->priority)
->orderBy('created_at', $orderBy)
->paginate($count);
return $mails;
}

Related

How to make an SQL statement that brings only the record that matches the two parameters provided?

Let's suppose I have a table called Diseases, with one column:
"disease_name" (primary Key)
And on the other hand I have a table called Symptoms, with three columns:
"id" (primary key auto generated number)
"symptom_name"
"disease_name_fk" (foreign key from table Diseases "disease_name")
Given the schema above, I want to retrieve the disease based on the symptoms.
For example I have the symptoms "fever" and " body pain", I want to retrieve only the diseases that matches those two symptoms from table symptons. If the disease has only one matche then it is worthless, it has to match the two parameters given, returning the diseases names corresponding to those symptoms.
How can I do that?
You can use group by and having:
select disease_name_fk
from symptoms
where symptom_name in ('fever', 'body pain')
group by disease_name_fk
having count(*) = 2; -- number of symptoms in list

Assign unique ID to duplicates in Access

I had a very big excel spreadsheet that I moved into Access to try to deal with it easier. I'm very much a novice. I'm trying to use SQL via Access.
I need to assign a unique identifier to duplicates. I've seen people use DENSE_RANK in SQL but I can't get it to work in Access.
Here's what I'm trying to do: I have a large amount of patient and sample data (20k rows). My columns are called FULL_NAME, SAMPLE_NUM, and DATE_REC. Some patients have come in more than once and have multiple samples. I want to give each patient a unique ID that I want to call PATIENT_ID.
I can't figure out how to do this, aside from typing it out on each row. I would greatly appreciate help as I really don't know what I'm doing and there is no one at my work who can help.
To illustrate the previous answers' textual explanation, consider the following SQL action queries which can be run in an Access query window one by one or as VBA string queries with DAO's CurrentDb.Execute or DoCmd.RunSQL. The ALTER statements can be done in MSAcecss.exe.
Create a Patients table (make-table query)
SELECT DISTINCT s.FULL_NAME INTO myPatientsTable
FROM mySamplesTable s
WHERE s.FULL_NAME IS NOT NULL;
Add an autonumber field to new Patients table as a Primary Key
ALTER TABLE myPatientsTable ADD COLUMN PATIENT_ID AUTOINCREMENT NOT NULL PRIMARY KEY;
Add a blank Patient_ID column to Samples table
ALTER TABLE mySamplesTable ADD COLUMN PATIENT_ID INTEGER;
Update Patient_ID Column in Samples table using FULL_NAME field
UPDATE mySamplesTable s
INNER JOIN myPatientsTable p
ON s.[FULL_NAME] = p.[FULL_NAME]
SET s.PATIENT_ID = p.PATIENT_ID;
Maintain third-norm principles of relational databases and remove FULL_NAME field from Samples table
ALTER TABLE mySamplesTable DROP COLUMN FULL_NAME;
Then in a separate query, add a foreign key constraint on PATIENT_ID
ALTER TABLE mySamplesTable
ADD CONSTRAINT PatientRelationship
FOREIGN KEY (PATIENT_ID)
REFERENCES myPatientsTable (PATIENT_ID);
Sounds like FULL_NAME is currently the unique identifier. However, names make very poor unique identifiers and name parts should be in separate fields. Are you sure you don't have multiple patients with same name, e.g. John Smith?
You need a PatientInfo table and then the SampleData table. Do a query that pulls DISTINCT patient info (apparently this is only one field - FULL_NAME) and create a table that generates unique ID with autonumber field. Then build a query that joins tables on the two FULL_Name fields and updates a new field in SampleData called PatientID. Delete the FULL_Name field from SampleData.
The command to number rows in your table is [1]
ALTER TABLE MyTable ADD COLUMN ID AUTOINCREMENT;
Anyway as June7 pointed out it might not be a good idea to combine records just based on patient name as there might be duplicates. Better way will be treat each record as unique patient for now and have a way to fix patient ID when patient comes back. I would suggest to go this way:
create two new columns in your samples table
ID with autoincrement as per query above
patientID where you will copy values from ID column - for now they will be same. But in future they will diverge
copy columns patientID and patientName into separate table patients
now you can delete patientName column from samples table
add column imported to patients table to indicate, that there might be some other records that belong to this patient.
when patients come back you open his record, update all other info like address, phone, ... and look for all possible samples record that belong to him. If so, then fix patient id in those records.
Now you can switch imported indicator because this patient data are up to date.
After fixing patientID for samples records. You will end up with patients with no record in samples table. So you can go and delete them.
Unless you already have a natural key you will be corrupting this data when you run the distinct query and build a key from it. From your posting I would guess a natural key would be SAMPLE_NUM. Another problem is that if you roll up by last name you will almost certainly be combining different patients into one.

Query to retrieve data using two foreign keys

I'm working on a football statistics database, and in the table to store results of matches, I have two references to the primary key of a team table: one home, one away.
My intention is to create a query which returns the name of both of the teams, along with other details, but I can't think of a way to achieve this WITH the team names (my attempts so far can only produce one team name, with the other an ID number). I'll give the relation structure if this wasn't clear:
(PKs in bold, FKs asterisk)
team(team_id, team_name, venue)
match(match_id, home_team*, away_team*, home_score, away_score, date,)
My desired output would be a table with these columns:
home_team_name, home_team_score, away_team_score, away_team_name, date, venue
Is this possible with my tables, or should I change the way I store results?
When joining the team table to the match table in a query, you'll need to join the match table to the team table twice. You need to use an different alias for the teams each time.

decompose source data into new, many-to-many schema

I am using MS Access 2010 to do some transformations of data. Specifically, I need to create the data structure for a many-to-many relationship between concept (summarized by rxnconso.rxcui) and word (summarized by drugwords.id. Note that each value of drugwords.id needs to correspond with a unique value of name from the words table in the images below.). To accomplish this, I need to create two tables, drugwords and drugwordsConsoJunction, and also decompose the contents of an existing table words into the drugwords and drugwordsConsoJunction tables. The structure of the destination tables is:
drugwords table: (this table needs to be created)
id (autonumber pk needs to be created from distinct values of words.name)
name
drugwordsConsoJunction: (this table needs to be created)
word_id (fk to drugwords.id)
rxcui (fk to rxnconso.rxcui)
rxnconso (this table already exists):
rxcui
...other fields
The source table for this transformation is called words and has two columns; a value for rxcui, and a value for name. As you can see from the images below, there can be many name values for a given rxcui value. And the second image below shows that there can be many rxcui values for a given name value.
How do I write the SQL to transform words into drugwords and drugwordsConsoJunction, as per the above specifications?
I have uploaded a copy of the database to a file sharing site. You can download it at this link.
If the proposed [drugwords] table is already going to have unique values in its [name] column then you don't need an AutoNumber ID column, you can just use the [name] field as a Primary Key. In that case, the table that maps "words" to the corresponding [rxcui] values could be created by simply doing
SELECT DISTINCT rxcui, [name] INTO drugwordsConsoJunction FROM words
Then you can use the "words" themselves instead of introducing another layer of mapping from (distinct) "words" to (distinct) "IDs".

MS Access 2007 - Semi-Autonumber

I am building my own CRM access database that will contain a list of contacts. I would like to have an opportunity reference one contact as an employee, and another as the customer. Is there a way that I can add employees using their internal user ID (always 4 digits) and autonumber customer contacts?
I don't plan on building forms until I am comfortable that everything is working properly in table view.
You cannot assign an ID to an autonumber field. I might suggest adding an employeeID field to your contacts table that would have the employee ID number. The contact ID would still be an autonumber, so in essence they would have two IDs, but their employeeID would need to be stored in a different field.
You can try something different, but you will likely run into a duplicate ID at some point. Use a seperate table for autonumber Customer IDs. When adding a customer into your contact table, you "insert" a new record into the separate customer ID table to get the autonumber ID. If you're inserting an employee, just use the EmployeeID as the contact ID. So you need to be very careful when mixing two ID systems. It's best to use 1 ID and then use the other ID in some other field.
Consider using two tables, one for contact fields and another for employee-specific fields. Put your AutoNumber field in the contact table, and put the employee id in the employee table. In the employee table, use a foreign key that refers to the AutoNumber field in the contact table.