Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I don't know if the title makes any sense, but here is the situation: See 4 tables below
User Table(There is 200 + user)
+-------+---------+-------------------+---------+
|iduser | user | address | idcity |
+-------+---------+-------------------+---------+
| 1 | person1 | some address | 123 |
| 2 | person2 | another address | 542 |
| 3 | person3 | different address | 623 |
+-------+---------+-------------------+---------+
City Table
+---------+-------+----------+
| idcity | city | idstate |
+---------+-------+----------+
| 123 | city1 | 1 |
| 542 | city2 | 2 |
| 623 | city3 | 3 |
+---------+-------+----------+
State Table
+---------+--------+------------+
| idstate | state | idcountry |
+---------+--------+------------+
| 1 | state1 | 4 |
| 2 | state2 | 5 |
| 3 | state3 | 6 |
+---------+--------+------------+
Country Table
+-----------+----------+
| idcountry | country |
+-----------+----------+
| 4 | country1 |
| 5 | country2 |
| 6 | country3 |
+-----------+----------+
NOTE = I need to find out all people that belong to State and search by idcity
Output
+--------+----------+-------------------+---------+---------+
| iduser | user | address | idcity | state |
+--------+----------+-------------------+---------+---------+
| 11 | person11 | some address | 123 | state1 |
| 2 | person2 | another address | 123 | state1 |
| 13 | person13 | different address | 123 | state1 |
+--------+----------+-------------------+---------+---------+
or
+--------+----------+-------------------+---------+---------+
| iduser | user | address | idcity | state |
+--------+----------+-------------------+---------+---------+
| 20 | person20 | some address | 542 | state3 |
| 12 | person12 | another address | 542 | state3 |
| 33 | person33 | different address | 542 | state3 |
+--------+----------+-------------------+---------+---------+
How to achieve this with a single JOIN?I tried combining the two table within a JOIN(in query) which doesn't feel right somehow. Please Help how can i fix this pblm. Thankyou
Try like this,
select u.iduser,u.user,u.address,u.idcity,s.state
From usertable u
inner join city c on c.idcity=u.idcity
inner join state s on s.idstate=c.idstate
To get the city id
select
c.idcity
from city c
where c.idcity = <someidcity>
To join state name
select
c.idcity,
s.state
from city c
join state s on s.idstate = c.idstate
where c.idcity = <someidcity>
Then to join user
select
u.iduser,
u.user,
u.address,
c.idcity,
s.state
from city c
join state s on s.idstate = c.idstate
join user u on u.idcity = c.idcity
where c.idcity = <someidcity>
You can't do that in a single join, as you wanna infos (other than ids) from table user and table state, which have no direct relationship.
Try this
SELECT
a.iduser,
a.user,
a.address,
b.idcity,
c.state
FROM
users a
INNER JOIN
cities b
ON
a.idcity=b.idcity
INNER JOIN
states c
ON
b.idstate=c.idstate
WHERE
c.state='myState'
AND b.city='myCity'
Related
I normalize my MySQL database which makes me unable to fetch accurate data, actually I need to get detail information about vendors. I have 5 tables. First table is "vendor" in which I have vendor basic info like name, email, address like shown below:
| id | name | email | address |
|------------|------------------|----------------------------------
| 1 | Haris | Haris#gmail.com | Abcd Efgh |
|------------|------------------|----------------------------------
| 2 | John | john#gmail.com | Abcd Efgh |
|------------|------------------|----------------------------------
| 3 | Chris | chris#gmail.com | Abcd Efgh |
Second table "materials" I have a list of materials in which I have two column id and material like shown below:
| id | materials |
|------------|------------------|
| 1 | Iron |
|------------|------------------|
| 2 | Plastic |
|------------|------------------|
| 3 | Steel |
Third Table is "categories" in which I have a list of categories id and categories like shown below:
| id | categories |
|------------|------------------|
| 1 | chair |
|------------|------------------|
| 2 | Table |
|------------|------------------|
| 3 | Glass |
In the fourth table which named "vendors_materials" I have vendor id, vendor name and material code.
| id | name | material_cod |
|------------|------------------|--------------------
| 1 | Haris | 1 |
|------------|------------------|--------------------
| 1 | Haris | 2 |
|------------|------------------|--------------------
| 3 | Chris | 1 |
and In the last fifth table which named "vendors_category" I have vendor id, vendor name and category code.
| id | name | category_cod |
|------------|------------------|--------------------
| 2 | John | 1 |
|------------|------------------|--------------------
| 2 | John | 2 |
|------------|------------------|--------------------
| 1 | Haris | 1 |
I want to get detail of vendor along with materials and categories like shown below:
| id | name | email | address | material | category |
|-----|----------|-------------------|-----------------------------------------
| 1 | Haris | Haris#gmail.com | Abcd Efgh | Iron plastic| Chair
|-----|------------------|-----------------------------------------------------|
| 2 | John | john#gmail.com | Abcd Efgh | | Chair table
|-----|------------------|-----------------------------------------------------|
| 3 | Chris | chris#gmail.com | Abcd Efgh | Iron | |
I have done lot of attempts to achieve my results but failed.
SELECT
vendor.name,
vendor.email,
vendor.address,
materials.materials AS material,
categories.categories AS category
FROM vendor
INNER JOIN vendors_materials ON vendor.name = vendors_category.name
INNER JOIN categories ON vendors_category.category_cod = categories.id
INNER JOIN vendors_materials ON vendors_materials.name = vendor.name
INNER JOIN materials ON materials.id = vendors_materials.material_code
also it is not well normalized database structure, first of all it is not recommended to connect tables with varchar columns when you can to connect with ints. For example your 'connector' tables vendors_materials and vendors_category is connected to vendor by name when you can connect by vendor.id. So your vendors_materials table should look like:
| id | vendor_id | material_cod |
|------------|------------------|--------------------
| 1 | 1 | 1 |
|------------|------------------|--------------------
| 1 | 1 | 2 |
|------------|------------------|--------------------
| 3 | 3 | 1 |
you can do the same in case of vendors_category.
Try following query:
Actually you need to learn use of JOIN in SQL. If you learn this thing then you will be great to make hard queries.
For reference you can go on : SQL Joins
SELECT vendor.name,
vendor.email,
vendor.address,
materials.materials,
categories.categories
FROM vendor
JOIN materials
JOIN categories
JOIN vendors_materials
JOIN vendors_category ON materials.id = materials.material_cod
AND categories.id = vendors_category.material
AND materials.id = vendor.id
AND vendor.id = categories.id
GROUP BY name;
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have tables like this
Employee
Id | IdEmployee | Employee | Address | Phone_num |
1 | EM10000 | Jack | wall st. | 9000010 |
2 | EM10001 | Paul | elm st. | 9000010 |
3 | EM10002 | George | ex st . | 9000010 |
Technician
id | IdTech | TechName | Address | phone_num |
1 | TE10000 | Gabut | Hello st. | 9000010 |
2 | TE10001 | Polnaref | coding st. | 9000010 |
3 | TE10002 | Rafioli | stack st. | 9000010 |
Admin
id | IdAdmin | username | password | phone_num |
1 | AM10000 | sim1 | 1234 | 9000010 |
2 | AM10001 | sim2 | luvcoding | 9000010 |
3 | AM10002 | sim3 | okaymate | 9000010 |
How do i make Table Technician and Employee become a user too, so they could use their information as username in a login form? Plus, i want each Id (not the "AutoIncrement" ID) of Technician and Employee would become the username.
How to manage the database like this?
or should i add password column in both tables? (Employee and Technician)
or should i make one table just like this?
(new) Admin
id | Id | username | password | phone_num |
1 | AM10000 | sim1 | 1234 | 9000010 |
2 | AM10001 | sim2 | luvcoding | 9000010 |
3 | AM10002 | sim3 | okaymate | 9000010 |
4 | AM10003 | EM10000 | test1 | NULL |
5 | AM10004 | EM10001 | test2 | NULL |
6 | AM10005 | EM10002 | test3 | NULL |
7 | AM10006 | TE10000 | 1234 | NULL |
8 | AM10007 | TE10001 | ok123 | NULL |
There is no relationship between Technician and Employee (stand-alone table)
In your case, you would be better making your tables in normalization form as follows:
Employees
IdEmployee | Employee | Address | Phone_num
EM10000 | Jack | wall st. | 9000010
EM10001 | Paul | elm st. | 9000010
EM10002 | George | ex st . | 9000010
Technicians
IdEmployee | TechName
TE10000 | Gabut
TE10001 | Polnaref
Admins
IdEmployee | Password
TE10001 | hashed_pass1
EM10002 | hashed_pass2
This makes sure you have no replicated data and ensures you can associate tables correctly by using foreign keys.
So if your admin has IdAdmin 'EM10002', to get the password of that employee with the following query:
// Will return a row with EM10002's password, which is 'hashed_pass2'.
SELECT Password from Admins WHERE IdEmployee = EM10002
Here is another example of foreign key usage.
To understand normalization more, have a look at this thread.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a Mysql query:
SELECT
`pcm`.`ContactMechanismId` AS `ContactMechanismId`,
`tel`.`AreaCode` AS `AreaCode`,
`tel`.`PhoneNbr` AS `PhoneNbr`,
`tel`.`Extension` AS `Extension`,
`cmt`.`ContactMechanismTypeName` AS `ContactMechanismTypeName`,
`c`.`DialingCode` AS `DialingCode`,
`rt`.`RoleTypeName` AS `RoleTypeName`,
`cmpt`.`ContactMechanismPurposeTypeName` AS `ContactMechanismPurposeTypeName`
FROM
`PartyContactMechanisms` AS `pcm`
INNER JOIN
`Telephones` AS `tel` ON `pcm`.`ContactMechanismId` = `tel`.`ContactMechanismId`
INNER JOIN
`ContactMechanisms` AS `cm` ON `cm`.`ContactMechanismId` = `pcm`.`ContactMechanismId`
INNER JOIN
`ContactMechanismTypes` AS `cmt` ON `cmt`.`ContactMechanismTypeId` = `cm`.`ContactMechanismTypeId`
INNER JOIN
`Countries` AS `c` ON `tel`.`GeoId` = `c`.`GeoId`
INNER JOIN
`PartyContactMechanismPurposes` AS `pcmp` ON `pcm`.`ContactMechanismId` = `pcmp`.`ContactMechanismId`
INNER JOIN
`RoleTypes` AS `rt` ON `rt`.`RoleTypeId` = `pcm`.`RoleTypeId`
INNER JOIN
`ContactMechanismPurposeTypes` AS `cmpt` ON `pcmp`.`PurposeTypeId` = `cmpt`.`PurposeTypeId`
The result I get from Mysql is
+--------------------+----------+----------+-----------+--------------------------+-------------+---------------+---------------------------------+
| ContactMechanismId | AreaCode | PhoneNbr | Extension | ContactMechanismTypeName | DialingCode | RoleTypeName | ContactMechanismPurposeTypeName |
+--------------------+----------+----------+-----------+--------------------------+-------------+---------------+---------------------------------+
| 2 | 111 | 22222222 | 2222 | landline | 375 | customer | common phone |
| 2 | 111 | 22222222 | 2222 | landline | 375 | contractor | common phone |
| 2 | 111 | 22222222 | 2222 | landline | 375 | manufacturer | common phone |
| 2 | 111 | 22222222 | 2222 | landline | 375 | customer | primary phone |
| 2 | 111 | 22222222 | 2222 | landline | 375 | contractor | primary phone |
| 2 | 111 | 22222222 | 2222 | landline | 375 | manufacturer | primary phone |
| 2 | 111 | 22222222 | 2222 | landline | 375 | customer | secretary phone |
| 2 | 111 | 22222222 | 2222 | landline | 375 | contractor | secretary phone |
| 2 | 111 | 22222222 | 2222 | landline | 375 | manufacturer | secretary phone |
| 1 | 17 | 2905950 | | landline | 375 | customer | other phone |
+--------------------+----------+----------+-----------+--------------------------+-------------+---------------+---------------------------------+
I need is to convert the above result into the following array:
[
[ContactMechanismId, AreaCode, PhoneNbr, Extension, ContactMechanismTypeName, DialingCode, RoleTypeName[], ContactMechanismPurposeTypeName[]
]
In the arrays RoleTypeName[] and ContactMechanismPurposeTypeName[] go ALL possible values for a certain ContactMechanismId from the corresponding column of the mysql result.
How can I do this?
You can change the select statement like bellow using GROUP_CONCAT()
SELECT
`pcm`.`ContactMechanismId` AS `ContactMechanismId`,
`tel`.`AreaCode` AS `AreaCode`,
`tel`.`PhoneNbr` AS `PhoneNbr`,
`tel`.`Extension` AS `Extension`,
`cmt`.`ContactMechanismTypeName` AS `ContactMechanismTypeName`,
`c`.`DialingCode` AS `DialingCode`,GROUP_CONCAT(
`rt`.`RoleTypeName`) AS `RoleTypeName`,GROUP_CONCAT(
`cmpt`.`ContactMechanismPurposeTypeName`) AS `ContactMechanismPurposeTypeName`
Then group the data using GROUP BY,You will get RoleTypeName and ContactMechanismPurposeTypeName as a comma seperated value.You can iterate using php loop and using explode() function to form the array
I have three tables and they are the following
User Table
+---------+-----------+--------+
| user_id | user_name | branch |
+---------+-----------+--------+
| 1 | John | 1 |
| 2 | Jim | 2 |
| 3 | Jern | 3 |
| 4 | Jack | 1 |
| 5 | Jery | 2 |
| 6 | Tom | 3 |
| 7 | Sona | 1 |
| 8 | Tina | 3 |
+---------+-----------+--------+
Branch Table
+-----------+----------------+
| branch_id | branch_name |
+-----------+----------------+
| 1 | IT |
| 2 | SALES |
| 3 | Administration |
+-----------+----------------+
Enquiry Table
+------------+---------------+---------+
| enquiry_id | enquiry_name | user_id |
+------------+---------------+---------+
| 1 | enqury_test1 | 1 |
| 2 | enqury_test2 | 2 |
| 3 | enqury_test3 | 1 |
| 4 | enqury_test4 | 3 |
| 5 | enqury_test5 | 2 |
| 6 | enqury_test6 | 5 |
| 7 | enqury_test7 | 1 |
| 8 | enqury_test8 | 2 |
| 9 | enqury_test9 | 4 |
| 10 | enqury_test10 | 6 |
| 11 | enqury_test11 | 2 |
| 12 | enqury_test12 | 7 |
+------------+---------------+---------+
From the above tables its clear that, each branch contains a number of users.
These users post multiple enquiries.
I need to get the total number of enquiries in each branch as
branch id => number of enquiries
I have tried various queries. But i couldn't get the result. Any one can help?
I am using MySQL and i need a single query to do this.
Thanks in advance
You need count and group by
select
b.branch_id,
count(e.user_id) as `total_enq`
from Branch b
left join User u on u.branch = b.branch_id
left join Enquiry e on e.user_id = u.user_id
group by b.branch_id
The query you have to perform to get you desired result is like this :-
$query = "SELECT u.branch, COUNT(u.user_id) AS `total_enquires`
FROM enquiry e INNER JOIN user u ON e.user_id = u.user_id
GROUP BY u.branch"
This will help you,and i think you don't need to join branch table as user table already contain branch_id.
This is the query
SELECT `branch`,`branch_name`,count(`user`.`user_id`),count(`enquiry_id`) FROM `user` inner join `branch` on `user`.`branch`=`branch`.`branch_id` inner join `enquiry` on `user`.`user_id`=`enquiry`.`user_id` group by `branch`
try it here
http://sqlfiddle.com/#!9/cf3eb/1
SELECT
bt.branch_id
,COUNT(enquiry_id) AS total_enquiry
FROM
enquiry_table et
INNER JOIN user_table ut on ut.user_id = et.user_id
INNER JOIN branch_table bt ON bt.branch_id = ut.branch
WHERE
1=1
GROUP BY
bt.branch_id
you can try this
If you have seen my previous requests you will see I am a TOTAL NOOB with php and mySQL. I wish I wasn't even doing this but I have been dumped in the deep end by people that have let me down. It is 1 day away from "completion" and I am left to pick up and mend the pieces of this system I have which doesn't fully function!
I have tried to explain this as logically and clearly as possible. If you need clarification please let me know and I will do my best!
Thanks in advance for any help as I am now desperate to get this thing done!
I have 3 issues!
ISSUE1
Matching Keys
(x)
TABLE1
| C_ID | C_Eth_O (x) |
+--------------------+-------------------------+
| 234 | 8 |
| 341 | 11 |
| 440 | 2 |
TABLE2
| Eth_ID (x) | C1_Eth_O |
+-----------------------+------------------------+
| 2 | Label2 |
| 8 | Label8 |
| 9 | Label9 |
| 11 | Label11 |
I need to list all the "C1_Eth_O" values in a multi select list. The user will make multiple selections from that list. When submitted I need to get the "Eth_ID" value(s) and return all the value(s) from TABLE1 where any of the selected options "C_Eth_O" = "Eth_ID". Each "C_ID" can only have one "C_Eth_O".
ISSUE2
Matching Keys
(x)
(o)
TABLE1
| C_ID (x) |
+---------------------+
| 234 |
| 341 |
| 440 |
TABLE3
| Ail_ID (o) | Ali_Label |
+-------------------------+-------------------------+
| 1 | Label1 |
| 2 | Label2 |
| 3 | Label3 |
| 4 | Label4 |
| 5 | Label5 |
| 6 | Label6 |
TABLE4
| CA_ID | C1_ID (x) | Ail1_ID (o) |
+---------------------+------------------------+------------------------+
| 1 | 234 | 1 |
| 2 | 341 | 4 |
| 3 | 341 | 6 |
| 4 | 440 | 2 |
I need to list all the "Ali_Label" values from TABLE3 in a multi select list. The user will make multiple selections from that list. When submitted I need to get the "Ail_ID" value(s) from TABLE3 and return all the value(s) from TABLE1 where any of the selected match in TABLE4 "Ali1_ID" = "Ail_ID" & "C1_ID" = "C_ID". Each "C_ID" can have multiple "Ail_ID" values
ISSUE3
Matching Keys
(x)
(o)
(-)
TABLE1
| C_ID (x) |
+---------------------+
| 234 |
| 341 |
| 440 |
TABLE3
| Ail_ID (o) | Ali_Label |
+------------------------+-------------------------+
| 1 | Label1 |
| 2 | Label2 |
| 3 | Label3 |
| 4 | Label4 |
| 5 | Label5 |
| 6 | Label6 |
TABLE5
| R_ID | C1_ID (x) | Cf1_ID (-) |
+-----------------------+-----------------------+-----------------------+
| 1 | 234 | 768 |
| 2 | 234 | 854 |
| 3 | 234 | 768 |
| 4 | 440 | 854 |
TABLE6
| CA_ID | Cf_ID (-) | Ail1_ID (o) |
+---------------------+------------------------+------------------------+
| 1 | 768 | 1 |
| 2 | 854 | 4 |
| 3 | 768 | 6 |
| 4 | 880 | 2 |
I need to list all the "Ali_Label" values from TABLE3 in a multi select list. The user will make multiple selections from that list. When submitted I need to get the "Ail_ID" value(s) from TABLE3 and return all the value(s) from TABLE1 where any of the selected "Ali_ID" = "Ail1_ID" & "C1_ID" = "C_ID" & "Cf1_ID" = "Cf_ID". Each "C_ID" can have multiple "Cf1_ID" values AND each "Cf_ID" can have multiple "Ail1_ID" values.
I have the system working returning simple queries such as
SELECT * FROM table1 WHERE C_ID = 234
but nothing with multiple tables and multiple results per C_ID!
Thanks again!
ISSUE 1 -
SELECT * FROM table1 INNER JOIN table 2 ON table1.C_Eth_O = table2.Eth_ID
ISSUE 2 -
SELECT * FROM table1 INNER JOIN table4 ON table1.C_ID = table4.C1_ID INNER JOIN table3 ON table3.Ail_ID = table4.Ail1_ID
ISSUE 3 -
SELECT * FROM table1 INNER JOIN table5 ON table1.C_ID = table5.C1_ID INNER JOIN table6 ON table5.Cf1_ID = table6.Cf_ID INNER JOIN table3 ON table3.Ail_ID = table6.Ail1_ID