querying values from several tables mysql - mysql

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.

Related

Get Data From Multiple Tables On the Basis of Column Value From Different Table

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.

Retrieve All From Multiple Tables

I have 2 tables:
Appointments
id serviceId
1 1
2 1
Services
id
1
The appointments table is linked to the services table by use of the serviceId column. When retrieving data from the appointments table, i'd like to also retrieve the data from the services table, but the number of columns in each table does not match.
I've tried this:
SELECT appointments.*, services.* FROM appointments
INNER JOIN services ON id = appointments.serviceId
WHERE id = ?
But this does not work. First of all, in the join, how can I reference an ID from the appointments table that I have not even retrieved the data for yet? Second, how can I get all the data from both tables and then retrieve the data if the two column names match?
This won't work:
results.getInt("id");
Because both tables have an id field.
specify the tables name in which ID belongs,
SELECT appointments.*, services.*
FROM appointments
INNER JOIN services
ON Services.id = appointments.serviceId
-- WHERE id = ? -- specify also here as the two tables have column ID
or add ALIAS around them
SELECT appointments.id as AppID,
appointments.serviceId as AppServiceID
services.id AS ServiceID
FROM appointments
INNER JOIN services
ON Services.id = appointments.serviceId
-- WHERE id = ?
when you get their values, use their alias now, something like this,
results.getInt("AppID");

custom made order by mysql

I have a row of entities stored in a mysql table;
entity table
order
id
name
type (refers to Type(id))
and a row of types stored in another table
Type table
id
name
order
the ORDER column in Type table specifies by what order the entities should be sorted - some should be ordered by name and some by id.
How do I create a mysql query that gets the ORDER BY clause from the type table and sorts the entities in the entity table by the ORDER stored for that entity type in the Type table
for example, let us say I have the following rows:
Entity table:
row 1:
id = 1
name = Virginia
type = 1
row 2:
id = 2
name = Virginia
type = 1
row 3:
id = 3
name = Canada
type = 2
types (rows in Type table)
row 1
id = 1
name = states
order = "name"
row 2:
id = 2
name = countries
order = id
I want to do the following query
SELECT entities.id, entities.name FROM entities INNER JOIN type ON entities.type = type.id ORDER BY ....
in the ORDER BY I want to order the entities based on what is stored in the ORDER row in the type table. So countries should be sorted by Entity(ID) and states should be sorted by Entity(name). How can I do that?
This doesn't seem like a very good design for a database. For your example, I would suggest something more similar to this:
CREATE TABLE countries (
countryID INT NOT NULL,
countryName VARCHAR(30)
);
CREATE TABLE states (
stateID INT NOT NULL,
countryID INT,
stateName VARCHAR(30)
);
Then you can perform queries like:
SELECT c.countryName, s.stateName
FROM countries c LEFT JOIN states s ON c.countryID = s.countryID
ORDER BY countryName, stateName;
At the very least, I would suggest using more obvious names for your columns, like in your entity table, you have a column named 'type' which refers to the 'id' field in the type table. Perhaps name them both typeID or something more obvious.
I also think it's a bad idea to create a column that stores information about which column to order by. Not only does that mean that you'll have to execute two queries every time (one to fetch the order by, and one to fetch the actual data), but you will also be storing a lot of extra data unnecessarily.

reading data from three tables using mysql

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

mysql database join based on a mapping

An abstraction of the problem is like this:
I have one table having a column called 'country'. the value stored are name of the country, e.g. US, UK..
I have another table having a column called 'country_code'. the value stored are numerical representations of the country, e.g. 12, 17...
how can I perform a join operation (e.g. inner join) based on these 2 tables? the difficulty is that the country and country_code has a one-to-one mapping but not directly equal to each other.
You could create a Mapping table containging the country and the country_code.
I assume you cannot change the table containing the country_code to use the string representation from country, or add an int column to your countries table?
Something like
country_mappings
country varchar column
country_code int column
PRIMARY KEY country, country_mapping
'
SELECT *
FROM countries c INNER JOIN
country_mappings cm ON c.country = cm.country inner join
your_other_table yot ON cm.country_code = yot.country_code