I have to join the data from two tables.
There are three table record,address and names.Id is the primary key in record table and foreign key in address and names table.There can be multiple name records for particular record.
RECORD(id (PK),text,search)
address(address_id(PK),type,street,city, id (FK))
name(name_id(PK),name,id (FK))
I want to get the data from these three tables in a way so that i will get the only first name record for each record id,not all the name records for record id's.
insert into RECORD values (1,record1,record1search);
insert into RECORD values (2,record2,record2search);
insert into name values (1,xxx,1);
insert into name values name(2,yyy,1)
insert into name values name(3,zzz,2)
i want to retriev the values in way:
record_id,text,namename_id:
1,record1,xxx,1
2,record2,zzz,3
I want the only first name record not the second or thirrd name record for particular record.
please help.
You will need an interim query on name by the record ID to find the FIRST ID and use THAT as basis to join to the actual name table. Also, you didn't provide any sample data for your address table, but that would be done in a similar fashion as the first names query performed here. Also, would the address table be associated to a specific named person and not just the ID? Will THAT be 1:many relationship to the Record ID?
select
r.id,
r.text,
n2.name,
n2.name_id
from
Record r,
join ( select n.id, min( n.name_id ) FirstNameID
from name n
group by n.id ) FirstNames
on r.id = FirstNames.id
join name n2
on FirstNames.FirstNameID = n2.name_id
SELECT RECORD.id, RECORD.text, name.name, name.name_id
FROM RECORD
LEFT JOIN address
ON address.id = RECORD.id
LEFT JOIN name
ON name.id = RECORD.id
GROUP BY RECORD.id
Related
I have one Table of users in which their is LOGIN_ID, LOGIN_PASSWORD, and USER_ROLE.
I want to get Name of USERS From Different Table with single query.
e.g.
Login_ID: 1001 is from students so we have to get the name form students.
Login_ID: 235738932 is from Employees so we have to get name from Employees.
There is no Foreign Key Relation in users Tables because Login_id is from two different Tables.
Try:
SELECT
LOGIN_ID,
LOGIN_PASSWORD,
USER_ROLE,
CONCAT(COALESCE(students.name,''),COALESCE(employee.name,'')) as Name
FROM users
LEFT JOIN students ON students.Login_ID = users.Login_ID
LEFT JOIN employee ON employee.Login_ID = users.Login_ID
COALESCE(students.name,'') Will return the students name. If the name is not found, the values will be NULL, and COALESCE will change this to ''.
The same happend to the name of the employee. Both names are getting concatenated.
I have a MySQL database that's basically got this setup:
The customer records have many address records associated to them, and have an internal_id text value.
The orders also have a single ship to address record associated to them.
However, I had a bug, and I'm hoping to solve this issue without code.
I need to match the name, address_one and city against the address record on the customer (they should have only 1 match) and assign the internal_id from that to the address on the orders address record.
I've tried this, but it's not working so far:
UPDATE address AS addr
LEFT JOIN Orders AS O ON addr.id = O.ship_to_id
SET addr.`internal_id` = (
SELECT internal_id (
SELECT *
FROM address
WHERE address.name = addr.name
AND address.`address_line_one` = addr.`address_line_one`
AND address.`city` = addr.`city`
) AS a_int_id)
WHERE O.deleted_at IS NULL;
have you tried creating another table?
for example: customers_address_table
with columns:
cus_add_id pk
customer_id
address_id
then in your orders table:
ship_id pk
cus_add_id
Thank you for passing by. I know basic SQL syntax and I can't seem to find a way to accomplish this task.
I need to create a stored procedure which inserts data from one table in another, amid retrieving data from many others. Suppose I have these tables with columns:
users
name (varchar)
sub_id (int)
dub_id (int)
tmp_users
name (varchar)
sub_name (varchar)
dub_name (varchar)
subs
id (int)
name (varchar)
dubs
id (int)
name (varchar)
Translated in pseudocode I should do something like this:
INSERT INTO users (name, sub_id, dub_id)
ALL ROWS FROM tmp_users VALUES (
name = tmp_users.name,
sub_id = SELECT id FROM subs WHERE tmp_users.sub_name = subs.name,
dub_id = SELECT id FROM dubs WHERE tmp_users.dub_name = dubs.name,
)
In wording I need to insert into users all rows from tmp_users, to keep the col tmp_users.name, but retrieve the afferent ids of all other tables based on *_name column. How should I approach this task?
It seems like you are looking for the INSERT ... SELECT syntax:
INSERT INTO users (name, sub_id, dub_id)
SELECT
tu.name,
s.id,
d.id
FROM tmp_users tu
LEFT JOIN subs s ON s.name = tu.sub_name
LEFT JOIN dubs d ON d.name = tu.dub_name
This brings all rows from tmp_users, then attempt to recover the corresponding sub_id and dub_id. For each row returned by the select, a record is inserted in users. A good thing about this syntax is that you can run the select query independently first, to see what would be inserted.
I have 4 tables containing id and names from different fields, and a master table that contains only ids, i need to create a query that return the names.
This is the structure (simplified)
table region = columns id, name
table country = columns id, name
table ethnics = columns id, name
table religion = columns id, name
table master = columns region, country,ethnics, religion
table master contains ONLY ids for each column, and i need to return the names that matches those ids, but i can't create the proper JOIN syntax.
Any hint?
Try this:
select region.name, country.name, ethnics.name, religion.name
from master
join region on (region.id = master.region)
join country on (country.id = master.country)
join ethnics on (ethnics.id = master.ethnics)
join religion on (religion.id = master.religion)
Then you can add any where clauses that you might need to filter the results.
I am using MySQL version 5.6.
I have a table event that takes two ids and a date, and a table student that contains a column id. I want to insert two ids when the name match (WHERE firstname =.., AND lastname=..,). However, I don't quite know how to take id from two rows in one insert command. Can any one help me please?
Thanks,
Paul
If you are trying to insert into a table, then you want to use insert. To get data from another table or query, you would want the insert . . . select form.
From what you say, this seems something like what you want:
insert into event(id1, id2, date)
select s1.id, s2.id, now()
from student s1 cross join
student s2
where s1.id <> s2.id and
s1.firstname = firstname and s1.lastname = lastname and
s2.firstname = firstname and s2.lastname = lastname;
It would return all pairs of students where the names match (but don't have the same id).