hi i need to display values from multiple tables by a single mysql query by userid. i'm having 6 tables followings...
country: countryid, country
state: stateid, statename, countryid
city: cityid, city, stateid.
categories: categoryid, category_name.
sub_categories: sub_category_id, sub_category_name.
users: userid, username, countryid, stateid, city, category_id and sub_category_id.
Now i want to display all details by userid. i written query and after it displays only id's for country, state, category, and sub_category and not display their names. i'm using select statement and as well as join statement. but not get exact output. i'm have basic knowledge in joining tables query. please give idea or query to display my output.
You can use a join like this:
select
a.username,
b.country,
c.statename,
d.city,
e.category_name,
f.sub_category_name
from
users a
join country b
on a.countryid=b.countryid
join state c
on a.stateid=c.stateid
and a.countryid=c.countryid
join city d
on a.city=d.cityid
and a.stateid=d.stateid
join categories e
on a.category_id=e.categoryid
join sub_categories f
on a.sub_category_id=f.sub_category_id
I am using the users.city column name from your question here, is it really cityid though - that would match the rest of your column naming convention more.
The database structure could be better, also, always have uniform nomenclature across your tables, it is easier writing queries.
Something like this should suffice:
SELECT c.country, s.statename, ci.city, ca.category_name, sc.sub_category_name FROM country c, state s, city ci, categories ca, sub_categories sc, users u WHERE u.country.id = c.countryid, u.stateid = s.stateid, u.city = ci.city, u.category_id = ca.categoryid AND u.sub_category_id = sc.sub_category_id ORDER BY u.userid DESC;
Related
I have two tables named companies and jobs. They are related - jobs table has a foreign key company_id.
company table columns are: companyname, city, province etc.
jobs table columns: title, department, description etc
I want to search both tables in those columns and if the keyword has a match in jobs table get the related data from the company table, and if the match is in the company table, I want to get all the jobs related to the company.
As far as I know in order to search in both tables I have to use UNION, this works:
$sql= "(SELECT title, department FROM jobs WHERE title LIKE ? )
UNION
(SELECT companyname,city FROM companies WHERE companyname LIKE ?)";
I dont know how to get related data from the other table depending on the match. Is it possible with just one call using JOIN?
Thanks.
Use JOIN and OR condition:
SELECT jobs.title, jobs.department,
companies.companyname, companies.city
FROM jobs
JOIN companies ON companies.id = jobs.company_id
WHERE (jobs.title LIKE ? OR companies.companyname LIKE ?)
With UNION:
SELECT jobs.title, jobs.department,
companies.companyname, companies.city
FROM jobs
JOIN companies ON companies.id = jobs.company_id
WHERE jobs.title LIKE ?
UNION
SELECT jobs.title, jobs.department,
companies.companyname, companies.city
FROM jobs
JOIN companies ON companies.id = jobs.company_id
WHERE companies.companyname LIKE ?
Try this
SELECT a.title, a.department,c.companyname, c.city
FROM a
JOIN c ON c.id = a.company_id
WHERE (a.title LIKE 'title' OR c.companyname LIKE 'companyname')
I am just tinkering with SQL as I am trying to get into more complex statements. I don't know this qualifies for it or not but please guide how to go about it.
I have looked at JOINS and some question of Multiple Select Statements but unable to understand them correctly.
I have the following two tables:
emp table:
emp_id, name, address, org_id
books table:
id, emp_id, status, org_id
where emp_id in books table is foreign key referencing emp table.
I need to fetch all the records from books table of a particular org. But along with that I need to get all the data of respective employee like name, address along the result.
Please guide me in the right direction.
Thanks
Try this
select B.*,E.name,E.address from books B
inner join employee E
on B.emp_id=E.emp_id
where B.org_id=1;
I took value of of org_id as 1 for test purpose.
SELECT books.id, books.emp_id, books.status, books.org_id, emp.name, emp.address
FROM books
JOIN emp ON emp.emp_id = books.emp_id
WHERE books.org_id = '3'
As the other said, the org_id from emp table makes no sense. You must remove it from the table.
if you want all records from the books table try this
Select b.id,c.emp_id,b.status,e.name,e.address
from emp_table e
Left join books_table b
on(e.emp_id=b.emp_id)
where e.org_id='3' and b.org_id='3'
You can use this query this will gives you all book record with particular org_id along with all employee record
SELECT * FROM emp as e1
LEFT JOIN books as b1 ON e1.emp_id = b1.emp_id WHERE e1.org_id = 'YOUR_ID'
UNION ALL
SELECT * FROM emp as e1
RIGHT JOIN books as b1 ON e1.emp_id = b1.emp_id WHERE e1.org_id = 'YOUR_ID'
The following query should work for you:
SELECT b.id, b.status, b.org_id, e.emp_id, e.name, e.address
FROM Books b LEFT JOIN Employee e
ON e.emp_id = b.emp_id
WHERE b.org_id = 100
This would be the query for org 100.
It appears that your current schema is not normalized very well:
Employee: emp_id, name, address, org_id
Books: id, emp_id, status, org_id
The two tables both store the org_id, which presumably means the same thing.
This query might give you the results you want. You might want to learn more about joins in SQL. A LEFT JOIN gives you all records from one table, and matching records from another.
SELECT books.*,
emp.name AS emp_name,
emp.address AS emp_address,
emp.org_id AS emp_org_id
FROM books
LEFT JOIN emp ON books.emp_id = emp.emp_id
WHERE books.org_id=?
you can use this for all recors both tables :
select * from books
inner join emp on
books.emp_id=emp.emp_id
where books.emp_id=emp.emp_id
I would to select some data from mysql. However, some of the data stored in the table I am querying from are in codes and to get the text description I need to reference that data to another table.
TABLE: persons
SELECT id, first_name, last_name, address_code, customer_type_code
FROM persons
WHERE id = 1001
TABLE: ref_address
SELECT address_name FROM ref_address
WHERE address_code = 123
TABLE: ref_customer_type_code
SELECT customer_type_name FROM ref_customer_type_code
WHERE customer_type_code = 456
How can I combine all three queries together to return id, first_name, last_name, address_name, customer_type_name in one query instead of querying them 3 times like this?
Please read the reference manual for join.
In short, you need to define a relation between your tables (I use aliases just to make things a bit "cheaper" to write):
select p.id, p.first_name, p.last_name, p.address_code, p.customer_type_code
, ra.address_name
, rctc.customer_type_name
from persons as p
-- Join the persons table with the ref_address table,
-- using the address_code column of each table
inner join ref_adress as ra
on p.address_code = ra.address_code
-- Join the persons table with the ref_customer_type_code table
-- using the customer_type_code column of each table
inner join ref_customer_type_code as rctc
on p.customer_type_code = rctc.customer_type_code
where p.id = 1001
Notice that when you use multiple tables in a query it may be useful to define aliases to avoid having to write again and again the full name of the table. Also, it may be a good idea to explicitly specify each field's source table (by alias, if you are using it)
What you're looking for is a JOIN.
In a JOIN, you specify two tables and how they are related to one another. In a single SELECT statement, you can have multiple JOIN clauses.
SELECT
p.id, p.first_name, p.last_name, p.address_code, p.customer_type_code,
a.address_name,
t.customer_type_name
FROM
persons p
JOIN ref_address a
ON p.address_code = a.address_code
JOIN ref_customer_type_code t
ON p.customer_type_code = t.customer_type_code
WHERE
p.id = 1001
This query says that the table persons and ref_address should be linked, or "joined", by the related columns address_code which are available in each table. Same goes with the tables persons and ref_customer_type_code being linked by the columns customer_type_code.
Been bothered with this for awhile now and i think it might be how i have the joins set up.
I have two tables. Ones is called info which contains all of a users contact information. My second table called numbers has all the phonenumbers for different users. They are related by the primary id of info to the info_id of phonenumbers. I want them to join based on this relationship and I want all the phonenumbers under phonenumbers to join into the single phonenumbers column in info. The current join i am using is this.
SELECT phonenumbers p, info i FROM i.phonenumbers
INNER JOIN p.workphone
ON i.PID=p.info_id
INNER JOIN p.homephone
ON i.PID=p.info_id
INNER JOIN p.mobilephone
ON i.PID=p.info_id
all i get is the SELECT comman is deneied to user on database workphone that isnt evena database.
table info:
PID,
firstname,
lastname,
address,
email,
phonenumbers,
table phonenumbers:
PID,
workphone,
homephone,
mobilephone,
info_id,
The syntax for a join would be nice. All the tutorials just give examples and not an explanation of what the different pieces are.
JOIN syntax is
TYPE_OF_JOIN database.table ON field = field
Since you have
JOIN p.workphone ON i.PID = p.info_id
You're actually telling the DB to look for a database named p, which contains a table workphone.
Doesn't matter that you've created an alias p up in your SELECT fields list. That's a field alias, and they NOT the same as a table alias.
So, I understand how the relationships work in mysql but I'm having a hard time figuring out how its implemented in my code.
For example, say I have the 3 tables.
Table 1: users - user id, username, user city
Table 2: categories - category id, category name
Table 3: user_categories - user id, category id
If I were to query the database for every user that was in a particular city and list them out with the all of the categories they belong to... How would I do this? Would I need to loop through the results and do a separate query for each user, then list the results? Or, is there some magic query that will return a multidimensional array?
I believe the above would be many-to-many, correct me if I'm wrong....
EDIT In the user_categories table, a user can contain more than 1 category, I'm trying to figure out how to return all of them
Thanks!
You're absolutely right, it is a many-to-many query.
And from what I understand, what you're looking for is the ability to have some kind of hierarchical result to display, meaning for one user, have an array of all the categories he's assigned to...
Couple of things you could do:
Option 1: Query the users table:
SELECT u.user_id, u.username, u.user_city WHERE city = 'somecity';
From the results, get all the user_id's that match, put them in an array.
array(1,3,4,5)
Then execute a query by joining the 2 tables categories and user_categories, and passing the array as a comma separated list in a where in:
SELECT user_categories.user_id, categories.category_name
FROM user_categories INNER JOIN categories ON user_categories.category_id = categories.category_id
WHERE user_categories.user_id IN (1,3,4,5)
This will give you a list of user-id, category name that you can use in your script with the previous results to build your result set
option 2: my preferred, use MySQL's GROUP_CONCAT(http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat).
SELECT users.user_id, users.user_name, GROUP_CONCAT(categories.category_name) AS categories
FROM users
INNER JOIN user_categories ON users.id = users_categories.user_id
INNER JOIN categories ON user_categories.category_id = category.id
WHERE user.user_city = 'somecity'
GROUP BY user.user_id
This will return something like:
user_id username categories
1 u1 cat1, cat2, cat3
2 u2 cat1, cat3
You can specify the separator by using SEPARATOR in group_concat.
You need to JOIN the tables.
If I were to query the database for every user that was in a particular city and list them out with the all of the categories they belong to
SELECT *
FROM users
INNER JOIN user_categories
ON (user_id)
INNER JOIN categories
ON (category_id)
WHERE ...
You could try:
SELECT u.user_id, u.username, u.user_city, c.category_id, c.category_name
FROM users u
INNER JOIN user_categories uc ON u.user_id = uc.user_id
INNER JOIN categories c ON uc.category_id = c.category_id
WHERE u.user_city = 'Cityname';
I haven't tested this, and there might be a more efficient way to do it, but it should work.
If you are unfamiliar with joins in mysql, check this out.