Joining tables in MYSQL without a common column - mysql

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

Related

DISPLAY DATA FROM TWO SEPERATE TABLE 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'

What happen if two sql table have the same column name while trying to write a sql query from the two? [duplicate]

I have a table for users. But when a user makes any changes to their profile, I store them in a temp table until I approve them. The data then is copied over to the live table and deleted from the temp table.
What I want to achieve is that when viewing the data in the admin panel, or in the page where the user can double check before submitting, I want to write a single query that will allow me to fetch the data from both tables where the id in both equals $userid. Then I want to display them a table form, where old value appears in the left column and the new value appears in the right column.
I've found some sql solutions, but I'm not sure how to use them in php to echo the results as the columns in both have the same name.
Adding AS to a column name will allow you to alias it to a different name.
SELECT table1.name AS name1, table2.name AS name2, ...
FROM table1
INNER JOIN table2
ON ...
If you use the AS SQL keyword, you can rename a column just for that query's result.
SELECT
`member.uid`,
`member.column` AS `oldvalue`,
`edit.column` AS `newvalue`
FROM member, edit
WHERE
`member.uid` = $userId AND
`edit.uid` = $userId;
Something along those lines should work for you. Although SQL is not my strong point, so I'm pretty sure that this query would not work as is, even on a table with the correct fields and values.
Here is your required query.
Let suppose you have for example name field in two tables. Table one login and table 2 information. Now
SELECT login.name as LoginName , information.name InofName
FROM login left join information on information.user_id = login.id
Now you can use LoginName and InofName anywhere you need.
Use MySQL JOIN. And you can get all data from 2 tables in one mysql query.
SELECT * FROM `table1`
JOIN `table2` ON `table1`.`userid` = `table2`.`userid`
WHERE `table1`.`userid` = 1

How to force mysql to show schema name along with column names in a query?

I need to join 3 tables which have some columns with same name, like id and some foreign keys columns.
I make a select query and the results come with table names only. How to get results like "dbname"."columnname" in my queries so I can identify from which table is each columns without having to specify every columns in the query (using only an *)?
Note: I use Delphi with ZeosLib, so a solution using these tools would be OK as well. But I prefer to set this in the data base.
You have to create an alias for your field name in your query
SELECT a.ID, b.ID
FROM a
JOIN b
You need doblue quote " for field names with special characters, so change it to.
SELECT a.ID "a.ID", b.ID "b.ID"
OR
SELECT a.ID "MeaningfullName", b.ID "OtherName"
For example here I have two fields name "sent_dt" and change one to previous_time
SQL Fiddle Demo

How can I refer to a field being referred by the SELECT query?

I have 2 tables, Ongoing_Portfolio and Ongoing_Fees. Ongoing fees contains a foreign key for Ongoing-Portfolio. Below are the tables
Please have a look at the below code
SELECT Ongoing_Portfolio.*,
Ongoing_Fees.Ongoing_Net_Income FROM Ongoing_Portfolio
INNER JOIN Ongoing_Fees ON Ongoing_Fees.idPortfolio = Ongoing_Portfolio.idPortfolio
WHERE Ongoing_Portfolio.idPortfolio = 1
AND
Ongoing_Portfolio.idOngoing_Portfolio = ?
Can you see the ? mark there in the last row? What I wanted to do there is to refer the idOngoing_Portfolio field of the Ongoing_Fees which is being refereed by the query at the moment. In more detail, I need something like below.
AND
Ongoing_Portfolio.idOngoing_Portfolio = idOngoing_Fees of the Ongoing_Fees table where the query is currently accessing
How can I do this in mysql?
The direct answer to your question is to put the column in as you described it:
SELECT op.*,
Ongoing_Fees.Ongoing_Net_Income
FROM Ongoing_Portfolio op INNER JOIN
Ongoing_Fees onf
ON onf.idPortfolio = op.idPortfolio
WHERE op.idPortfolio = 1 AND
op.idOngoing_Portfolio = onf.idOngoing_Fees;
Does this do what you want? If not, you might consider asking another question. Putting the actual create table statements is more useful than a picture of two tables. Sample data and desired results are a big help both for you to understand what you want to do and to convey this information to others.

MySQL - is it possible to use a column as a tablename?

Suppose that we have, in mysql, a table "mytable"
with the columns: id, title, type.
Is it possible to use its column as a table name in the same query?
For example:
SELECT m.id, m.title FROM mytable m INNER JOIN m.type WHERE m.id=2
Where "type" will give me the name of the table to do the inner join.
No, Sorry :(
The closest you can get (afaik) is to cursor through your main table and write dynamic html for the join for each row. VERY slow.
Or, find a new design pattern - Is there scope for you to post a question about what you are trying to achieve and how people may go about that?
Why not put all of your type data in a single table ? and then partition the table by type.