DISPLAY DATA FROM TWO SEPERATE TABLE MYSQL - mysql

Im trying to DISPLAY all customer name and their phone no under the Agent named Santakumar which have AGENT_CODE = A010.
the customers data have different table and agents data have also in the different table. the only thing that connect to them is AGENT_CODE.
I try using this QUERY but nothing happens and give this error:
Error Code: 1052. Column 'AGENT_CODE' in where clause is ambiguous 0.000 sec
SELECT A.AGENT_NAME, C.CUST_NAME, C.PHONE_NO
FROM agents AS A
JOIN customer AS C ON A.AGENT_CODE = C.AGENT_CODE
WHERE AGENT_CODE = 'A010'
Im new to MYSql so im not really that skilled in this field.
Thank you for the help in advance.

Since AGENT_CODE exists in more than one table/alias, you have to specify to which one you are referring to in the where clause:
WHERE A.AGENT_CODE = 'A010'

Related

Joining tables in MYSQL without a common column

I have searched the answers on how to join two tables in MYSQL without a common column on internet but did not find what I was looking out exactly. So I have two tables as shown below.
TABLE userDetails
id empID Name
1 001 Rhonda
TABLE request
id ticketNo Details
1 00000123 {"userDetails":{"id":"1", "empID":"001", "Name":"Rhonda"}
Now what I want to do is I want to write a query that when I search for empId:001 I want that the table returns me with that ticket number for that particular EMPID. But the problem here is that there are no common columns and second that the details of that user in Table Request is under column details. I am stuck as to how to achieve this. If anyone could help out it would be of great help. Thanks in advance.
You can use ->> operator within the JOIN condition after fixing the JSON column format such as
converting it to the form {"userDetails":{"id":"1", "empID":"001", "Name":"Rhonda"}} :
SELECT ticketNo
FROM request r
JOIN userDetails u
ON r.Details ->> '$.userDetails.empID' = u.empID
Demo
Update : Perhaps, you can try to replace u.empID with CONVERT(u.empID USING utf8) or CONVERT(u.empID USING latin1) in order to catch the proper collation

I cannot get my joins to work on a some tables, but they will work on others. Trying to select columns from 2 tables

I am new to mySQL so sorry for the basic question,
When I try to join my rental table and tp_rental table via the 'Rental_ID' column I get errors saying Error code: 1052 Column 'Rental_ID' in field list is ambiguous.
i can join rental and member tables no problem.
i am trying to print out a transaction report when a rental is completed (game is returned) that it will show rental ID, memberID, Due Date and Date Returned.
I was using the below code but getting errors:
select Rental_ID,member_ID,completed,Date_Due,Date_Returned
from gamestoredb.rental
inner join gamestoredb.tp_rental
on rental.Rental_ID=tp_rental.Rental_ID
Rental_ID is a column in each of the two tables you are joining so the server does not know which one you want rental.Rental_ID or tp_rental.Rental_ID even though in this particular they both would have the same value. Make which one you want explicit, for example:
select tp_rental.Rental_ID,member_ID,completed,Date_Due,Date_Returned
from gamestoredb.rental
inner join gamestoredb.tp_rental
on rental.Rental_ID=tp_rental.Rental_ID

Update SQL record based on a record in another table

I'm trying to run a query, that compares two tables. I got a table called mw_email_blacklist, this table is holding about 200k records of my blacklisted emails. In my other table, I got my list subscribers, they got a 'status' field, which can equal "Blacklisted" and then they can't receive an email.
Both tables got a 'email' field, so my plan was to do a where clause based on the email field. This however is not really working out. Can somebody maybe help me?
Included SQL:
UPDATE
`mw_list_subscriber` mls,
`mw_email_blacklist` meb
SET
mls.`status` = "Blacklisted";
WHERE
mls.`email` = meb.`email`;
This SQL actually counted something that could be the right amount of emails that should be blacklisted, but I couldn't get it to work when writing it to an update clause.
SELECT count(ls.email)
FROM mw_list_subscriber ls
WHERE EXISTS (
select email from mw_email_blacklist eb
WHERE ls.email = eb.email
)
Thanks in advance!
If i understand your intention correctly, a general approach could be:
UPDATE mw_list_subscriber mls , mw_email_blacklist AS meb
SET mls.status='Blacklisted'
WHERE mls.email = meb.email

Query For Getting Value From two tables

Hi i need your help on MS Access Database. I have two tables,tblExpense with three Columns(ExpenseID,ExpenseDate,Status) And tblExpenseDetails(ExpenseID,RefNo,Amount,Purpose).
Which Select Statement should i use if i want to get the Status from tblexpense using the RefNo from tblExpenseDetails?
{SELECT tblexpenseDetails.RefNo,tblexpense.Status,tblexpenses.ExpenseDate FROM tblExpense INNER JOIN tblexpenseDeatils ON tblexpensedetails.ExpenseID=tblexpenses.ExpenseID WHERE tblexpenseDetails.RefNo=?;
Used That and it worked any other ideas are still welcome.

How to combine 5 tables together with same ID in a query?

I have 5 different tables T_DONOR, T_RECIPIENT_1, T_RECIPIENT_2, T_RECIPIENT_3, and T_RECIPIENT_4. All 5 tables have the same CONTACT_ID.
This is the T_DONOR table:
T_RECIPIENT_1:
T_RECIPIENT_2:
This is what I want the final table to look like with more recipients and their information to the right.
T_RECIPIENT_3 and T_RECIPIENT_4 are the same as T_RECIPIENT_1 and T_RECIPIENT_2 except that they have different RECIPIENT ID and different names. I want to combine all 5 of these tables so on one line I can have the DONOR_CONTACT_ID which his information, and then all of the Recipient's information.
The problem is that when I try to run a query, it does not work because not all of the Donors have all of the recipient fields filled, so the query will run and give a blank table. Some instances I have a Donor with 4 Recipients and other times I have a Donor with only 1 Recipient so this causes a problem. I've tried running queries where I connect them with the DONOR_CONTACT_ID but this will only work if all of the RECIPIENT fields are filled. Any suggestions on what to do? Is there a way I could manipulate this in VBA? I only know some VBA, I'm not an expert.
First I think you want all rows from T_DONOR. And then you want to pull in information from the recipient tables when they include DONOR_CONTACT_ID matches. If that is correct, LEFT JOIN T_DONOR to the other tables.
Start with a simpler set of fields; you can add in the "name" fields after you get the joins set to correctly return the rest of the data you need.
SELECT
d.DONOR_CONTACT_ID,
r1.RECIPIENT_1,
r2.RECIPIENT_1
FROM
(T_DONOR AS d
LEFT JOIN T_RECIPIENT_1 AS r1
ON d.ORDER_NUMBER = r1.ORDER_NUMBER)
LEFT JOIN T_RECIPIENT_2 AS r2
ON d.ORDER_NUMBER = r2.ORDER_NUMBER;
Notice the parentheses in the FROM clause. The db engine requires them for any query which includes more than one join. If possible, set up your joins in Design View of the query designer. The query designer knows how to add parentheses to keep the db engine happy.
Here is a version without aliased table names in case it's easier to understand and set up in the query designer ...
SELECT
T_DONOR.DONOR_CONTACT_ID,
T_RECIPIENT_1.RECIPIENT_1,
T_RECIPIENT_2.RECIPIENT_1
FROM
(T_DONOR
LEFT JOIN T_RECIPIENT_1
ON T_DONOR.ORDER_NUMBER = T_RECIPIENT_1.ORDER_NUMBER)
LEFT JOIN T_RECIPIENT_2
ON T_DONOR.ORDER_NUMBER = T_RECIPIENT_2.ORDER_NUMBER;
SELECT T_DONOR.ORDER_NUMBER, T_DONOR.DONOR_CONTACT_ID, T_DONOR.FIRST_NAME, T_DONOR.LAST_NAME, T_RECIPIENT_1.RECIPIENT_1, T_RECIPIENT_1.FIRST_NAME, T_RECIPIENT_1.LASTNAME
FROM T_DONOR
JOIN T_RECIPIENT_1
ON T_DONOR.DONOR_CONTACT_ID = T_RECIPIENT_1.DONOR_CONTACT_ID
This shows you how to JOIN the first recipient table, you should be able to follow the same structure for the other three...