For some reason I feel this is a very easy task but I just can't get my head around it. I have two tables. Table A is list of companies called company and Table B is a list of users called users. The user table has a filed called 'access' which stores the ids of all the companies that a user is allowed to see.
companies
id name
-- ----
1 coca cola
2 samsung
3 apple
4 microsoft
users
id access name
-- ------ ----
1 1,3,4 brain owen
2 2,3 janet smith
2 1,2,4 peter pete
2 2,3,4 jane dow
My problem is how do I display the list of users who have access to a particular company? eg coca cola. Thank you.
id name
-- ----
1 coca cola
2 samsung
3 apple
4 microsoft
userId companyId
------ ---------
1 1
1 3
1 4
2 2
2 3
id name
-- ----
1 brain owen
2 janet smith
Two users can't have the same ID...
I think this will give you the user name of coca cola
select name from users where access in(1)
Related
Eg:
**Category Table**
Catg_id Catg_name
-------------------
1 Bike
2 Car
**Company Table**
Company_id Company_name
--------------------------
1 Bajaj
2 Honda
**Company_category table**
com_catg_id Company_id Category_id
---------------------------------------
1 1 1
2 2 1
3 2 2
** Models table**
Model_id Model_name com_catg_id
----------------------------------------
1 Pulsar 220 1
2 Unicorn 2
3 City 3
**Purchase Table***
Purchase_id Vehicle_No Rate model_id status
-------------------------------------------------------
1 KL 02 AN8306 50000 2 0
2 KL 10 AZ4764 120000 1 1
3 KL 04 AV8578 800000 3 1
These are 4 Database tables using.
I am using ajax for auto complete searching through a single field
eg: searching car, want to list all cars in purchase table of status 1
if searching bike, want to list all bike in purchase table of status 1
search using company name, want to list all vehicle from that company in purchase table of status 1
same as search using model name, vehicle no,rate want to list matched items in purchase table
Please help me and please send a mysql query for implementing this.
Check this tut
This would surely help you.
This tutorial is for single field. It isn't hard to modify the code and use for multiple fields
I am currently working on a system which supports "custom fields" for users.
I will illustrate this with a simplified example of this database schema:
users
--------
user_id
name
age
custom_fields
----------------
custom_field_id
custom_field_name
user_has_custom_field_value
------------------------
user_id (FK to users table)
custom_field_id (FK to custom_fields table)
custom_field_value (column that contains value of custom field)
This schema supports creation of custom fields to store information as per what the customer wishes.
However, I want to list all users along with their custom fields in the query output (or by using some PHP logic)
| user_id | name | age | fav_sport | payment_mode | bank
----------------------------------------------------------------------------------
3 John 28 Football SWIFT Chase
6 Jane 24 Swimming Cash BOA
fav_sport, payment_mode and bank are custom fields created which are represented as 3 rows in the custom_fields table.
So, the custom_fields table looks like this :
custom_field_id | custom_field_name
-----------------------------------------------
1 fav_sport
2 payment_mode
3 bank
and the user_has_custom_field value looks like this :
user_id | custom_field_id | custom_field_value
----------------------------------------------------------------------
1 1 Football
1 2 SWIFT
1 3 Chase
2 1 Swimming
2 2 Cash
2 3 BOA
What is the best way to achieve this with minimum queries and without taxing the database ?
The only solution I can come up with is by using a query inside a for loop in PHP to fetch the custom fields (which will definitely be heavy). What are more efficient solutions to do this ?
Suggestions welcome for MySQL based, PHP based, Stored Procedures etc.
I have to find a solution for my ranking scenario.
Say i have 100 users in database. And there is 5 subject in another table. In my project users have an option to rate 1 to 5 rating of any subjects. Means a user should have 5 different rating for 5 different subject. I need to shoe on the front page which is the most rated subject on the landing page.
I have a plan like create a table like
table name: user_subject_rate
Structure
id: user_id: subject1: subject2: subject3: subject4: subject5
1 1 3 1 2 5 4
When i create a table like this what happens if a new subject come to rate?
Can anybody suggest me a solution for this? I am using mysql as database.
I would advise you to structure your tables like below.
users
user_id | name
-----------------
1 Michael
2 Andrew
3 Annie
subjects
subject_id | name
----------------------
1 Maths
2 English
3 Physics
4 Chemistry
5 Biology
users_subjects_scores
user_id | subject_id | score
----------------------------
1 1 5
1 2 5
1 3 4
1 4 2
1 5 3
2 1 1
2 2 2
2 3 4
2 4 5
2 5 3
Then you can work out the total score of each subject using this query:
SELECT
name,
COALESCE(SUM(score), 0) AS total_score
FROM
subjects
LEFT JOIN
users_subjects_scores USING (subject_id)
GROUP BY
subject_id
ORDER BY
SUM(score) DESC
Then adding a new subject is as simple as adding a new row to the subjects table.
You can see an SQL fiddle here.
Basically, am working with a Human Resource Management application.
Positions (like trainer, deputy manager, manager, etc) are filled up based on their service.
There is a list of all employees, the one with highest service on any given date occupies the top-most position.
The number of key positions are fixed. Based on the employee's position in the list, his probable position will be displayed.
The positions are like:
(Position - Number of Posts)
Director - 1
Manager - 3
Dy. Manager - 5
Trainer - 10
Now, in the list of employees populated based on their experience, the first in the list will be the Director, next three will be Managers, following 5 employees will be Dy. Managers and next ten would be Trainers.
I want to design a database table for storing the positions and associated numbers. There may be possibility that new positions will be added and also, the number of positions may change with time. Example, if the number of Director posts are increased to two from one, it should be edited. As soon as the number of Director posts are edited, the first two employees in the list will be designated as probable Directors. The same logic holds good to all the other positions too.
Am not sure what should be the database structure for such a table. After done, I should be able to query the table using php for probable position of any given employee based on his rank in the employees list.
Example, if an employee is in rank 5, I should be able to query the table and get the probable position. In this case, rank 5 will be Dy. Manager.
Hope I have made my requirement clear. Please help me in designing a database table for this purpose.
Thank you!
Employees
=========
name | Boss
rank | 1
doj | 2010-01-01
"Select name from Employees order by rank, doj"
doj = date of joining
Is that what you mean?
rank could join to another table, where you store the readable rank title in order to generate title, name, etc
The existing employee database table consists of:
Employees
=========
name | Mark
doj | 2010-01-01
wrk | yes
"Select name from Employees where wrk=yes order by doj"
(this will remove employees in database who has left the company, with status wrk = no)
Lets say this will generate the results something like this:
Mark | 2010-01-01
Steve | 2010-02-01
Jack | 2010-04-01
Rick | 2010-09-01
David | 2010-12-01
Now, when I fetch the results and assign position based on their seniority (assuming positions manager=1, dy. manager=3, trainer=1) the result would be:
Rank | Name | Doj | Designation
1 | Mark | 2010-01-01 | Manager
2 | Steve | 2010-02-01 | Dy. Manager
3 | Jack | 2010-04-01 | Dy. Manager
4 | Rick | 2010-09-01 | Dy. Manager
5 | David | 2010-12-01 | Trainer
After some days, lets assume Jack quits the company (his wrk status will be no). So, the fetched results would look:
Rank | Name | Doj | Designation
1 | Mark | 2010-01-01 | Manager
2 | Steve | 2010-02-01 | Dy. Manager
3 | Rick | 2010-09-01 | Dy. Manager
4 | David | 2010-12-01 | Dy. Manager
As Jack quits, David gets promoted. The rank is just the serial number generated based on the number of rows fetched, auto increment.
Now, I wanted to create a table for the designation and number of positions, so that based on the rank, it fetches the designation.
Here's the problem:
The user will input his information on the required fields. Then the user will choose a selection in check boxes (the user can choose multiple) which will be saved in database (the value of the check box).
For example:
David selected 2 choices. Sports for example. David choose Basketball and Volleyball.
In my database, it would look like this:
| id | firstname | lastname | sports |
| 1 | David | White | Basketball |
| 2 | David | White | Volleyball |
My main problem is if I have to alter or delete the information, only one row will be altered or deleted which is a big problem. I can't make it like in explode or implode cause I have display the count of how many user have chosen a certain sport.
How am I able to solve this problem?
Looks like you need to normalize your table. You can create a table to store the person data:
person = (personid, firstname, lastname, other fields related to person)
and a table to store the choices:
sport = (sportid, name, other data related to sport)
And finally a table to manage the relationship:
PersonSport = (personid, sportid)
Then in the above scenario your data will be:
Person
personid Firstname Lastname
1 David White
2 Sam Black
and
Sport
sportid name
1 Basketball
2 Football
3 Tiddlywinks
and
PersonSport
Personid Sportid
1 1
1 2
2 3
2 1
To count how many users selected a sport:
Select count(*) from PersonSport where sportid = 1;