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
Related
Suppose i have some tables like...
countries
id name
1 Cyprus
2 India
states
id name country_id
1 a 1
2 b 2
3 c 2
cities
id name state_id
1 x 1
2 y 2
3 z 3
4 p 2
pages
id name slug status
1 ab a-b 1
2 pq p-q 0
3 abc a-b-c 1
mode_of_training
id name
1 Virtual
2 Classroom
items
id name description
1 a something
2 b something
prices
id price currency_code
1 200 USD
2 300 AUD
3 4000 INR
offers
id name discount
1 xyz-1 20%
2 abc-2 30%
3 pqr-3 10%
Creating table structure in that way is correct?? so that i can reuse them
using there ids.
For example
items_relation_table
id country_id state_id city_id page_id item_id price_id offer_id status
1 0 0 0 1 1 1 0 1
2 0 0 0 1 1 1 1 0
3 1 0 1 2 2 1 2 1
4 1 0 1 3 3 2 1 1
Now i don't need to use field values of tables
If i want to change price. i'll change price in one place only
I'm saving number of bytes.
database table size is less
But my problem is
To fetch data i need to use Joins
Or Creating View is better idea
Or There is any Better way to create table structure
Your entity-tables look fine at the first glance. But I don't understand your relation table. Looks like you try to relate everything with everything, which in some cases probably doesn't make sense (do offers really belong to countries?) in in some cases it seems redundant: When a page is linked to (many) cities, there is no need to link it to the countries too, because the countries are determined by the cities.
You should add relation-tables only between those entities that really need to be in a direct many-to-many relation. And for each of those relations you need a separate table.
e.g. for a relation between pages and cities:
cities(id, name, state_id) <--> cities_pages(city_id, page_id) <--> pages(id, name, slug, status)
And yes, you have to join tables to fetch data. Thats one of the basic ideas behind relational databases. Don't be afraid of joins, if your tables are properly indexed thats not an expensive operation at all (assuming that performance is your concern). And of course you could add some views if that makes sense for your application, but that will include the same JOINS you would just abstract them behind CREATE VIEW statements.
I'm trying to implementing faceted search in a Jewellery store, but failed. Problem is when try to filter the attributes table. structure is as follows:
Products Table:
Id Product_Code Product_Name
1 ABCGOLD1GM 1 gm Gold
2 ABCGOLD2GM 2 gm Gold
3 ABCGOLD394 3.94 gm Gold
Attributes Table:
Id Attr_Name Alias
1 Metal metal
2 Fineness fineness
3 Weight weight
Product_Attributes Table:
id product_id attr_id value
1 1 2 9999
2 1 3 1 gm
3 2 2 9999
4 2 3 2 gm
5 3 3 3.94 gm
6 3 2 9167
Now i want to filter if someone selects 1 gm with 999 and 3.94 gm. If you know faceted search then you can easily know what i mean. I want to make query to do the same with the table structure above.
I have messed with this query for 3 days now and no matter what I cannot get the right result. I'm working on a small database for a friend of mine for his mechanic shop.
I got 3 tables car, jobs and invoices. Everything start with "cars" if you add a car you get car_id for it. Next step would be to add a job to this car. Of course job goes to jobs table and also records car_id. After the job is done you can create an invoice out of it which goes to invoices table and again records car_id.
cars jobs invoices
=======================================================
cID, make, model | jID,cID,job1 | iID,jID,cID,amount
1 audi a6 | 1 1 check | 1 1 1 99.99
2 bmw 750 | 1 2 oil | 2 2 2 56.97
3 saab 95 | 3 3 oil | 3 3 3 30.22
1 audi a6 | 4 1 oli | 4 4 1 22.33
Small draft what the tables look like. And the outcome what I'd like to get should look like this:
ID make model
--------------------------
1. audi a6
Job 1 Invoice 1
Job 4 Invoice 4
2. bmw 750
Job 2 Invoice 2
3. Saab 95
Job 3 Invoice 3
Basically when I click my client list I get a table rows with clients and underneath client I'd like to print links to jobs and invoices.
Quite long post but I hope you understand what I mean.
I've tried different JOINs, the latest that I tried was
SELECT * FROM cars
RIGHT JOIN jobs ON cars.car_id=jobs.car_id
RIGHT JOIN invoices ON cars.car_id=invoices.car_id
ORDER BY cars.car_id;
It kind a worked but it gave me separate rows for each job with same client info. May I'm printing the result wrong. I just don't know any more. Can anybody offer a solution for this? right SQL command html for printing.
Why don't you just make it in a single table with fields like this:
id car job invoices
===============+===============+==============+==============+
1 + fastCar + works + 99.99 +
+ + + +
+ + + +
With statement like this:
CREATE TABLE cars (id int(11) AUTO_INCREMENT, car text(50) default '', job text(50) default '', invoices int(5) default NULL);
Then you can get all car attributes using a query like:
SELECT job FROM cars WHERE car='fastCar';
and pass desired values with another query to a teble that contains
details for the job for example, where you can have job id as needed.
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.
I'm a new in Mysql and I have a complicated problem:
I have a table with "Shops" name in this table there is a ShopID column. The records look like this:
Shop_001
Shop_002...
Every "shopID" refer to a new table with this name, for example there is a table with Shop_0001 name. In this table there is "partnumber" column which mean the parts which are available in this shop.
I send a specific part number to sql server and I want to check all shops in the "Shops" table and return a rows in the "Shop_xxxx" tables which has this specific partnumber.
Unfortunately I have no idea how do I get start on this. Can anybody help me give some instruction or anything on this?
you're looking for a many to many relationship. so you just need 3 tables
1 table is the list of shops
1 table is the list of products
and 1 table is the list of which shops have which products. like this
table1
id|shops
------
1 shop1
2 shop2
3 shop3
table2
id|products
------
1 prod1
2 prod2
3 prod3
4 prod4
5 prod5
table3
id|shop_id|prod_id
-------------------
1 2 3
2 2 1
3 2 2
4 1 3
5 1 4
6 1 5
7 3 2
So for every time a product is added to a shop, an entry is added in table3. This will allow you to query by shops or by products, and you will only ever need 3 tables.
google querying many to many relationships for how to get the list of products for shop1 or the list of shops that have product4 etc.