Mysql - find fields with same id - php

I need to contruct a query, from keywords entered by a user in a text-field, which will do the following: Take the keywords and search in a table, after which car has all the given keywords.
My table looks like this:
field_id | car_id | keyword |
----------------------------
1 | 5 | 1989 |
-----------------------------
2 | 8 | old |
-----------------------------
3 | 8 | ford |
-----------------------------
4 | 2 | audi |
-----------------------------
5 | 5 | red |
-----------------------------
6 | 8 | cheap |
-----------------------------
Say if the user searched for "old", "ford" and "cheap", the car_id 8 should be returned because it contains all the keywords. How would you pull this off?

Assumes you know the number of key words entered and you want an a match on all of them
In addition it assumes that a keyword, car_id are unique. (car_Id 8 can't have cheap twice)
Select car_ID from myTable
Where keyword in ('old','ford','cheap')
group by car_ID
having count(*) = 3

Related

Fetch results in group based on the occurrence

Ok, I have a single MySQL table with the name 'car' and 3 columns.
+----+--------+------------+
| ID | car_id | engine |
+----+--------+------------+
| 1 | 123 | on |
| 2 | 123 | on |
| 3 | 123 | off |
| 4 | 123 | on |
| 5 | 123 | on |
| 6 | 123 | on |
| 7 | 123 | off |
| 8 | 123 | on |
| 9 | 123 | off |
+----+--------+------------+
Now I want to show the trips this car did. The trips would be determined based on car engine start and stop. For example from the above example we can see that user has made 3 trips as total(From on to off). Now What I want is that if there is a query which gives me only 3 results from on to off meaning if somehow the query groups the records by considering a starting point on and ending point off. Is it possible in mysql? or the other way around is doing it manually by fetching all the records and working in arrays?
At the moment I am fetching all the records and doing it manually by looping all the data and doing accordingly But this process is slow.
Can you try it ?
SELECT * from cars WHERE `engine` = 'off' AND id IN(SELECT id+1 FROM `cars` WHERE `engine` = 'on')

sort results from database by cell value

i have database like this
============================
| id | name | value | key |
============================
| 1 | sara | | 1 |
============================
| 2 | sara | | 1 |
============================
| 3 | sara | 1 | |
============================
| 4 | jhon | | 1 |
============================
| 4 | jhon | 1 | |
============================
i want first to get only one result for each name
my expected output
jhon
sara
i use
select * from my_table
but it's display all names
and need to sort table by key cell
my expected output with sort
sara (3 keys)
jhon (1 key)
The function you are searching is called group by.
SELECT name, SUM(key) FROM my_table GROUP BY name
You can also use other aggregate function (not only SUM), maybe you want
SELECT name, COUNT(*) FROM my_table GROUP BY name
SELECT name, COUNT(key) FROM my_table GROUP BY name
Search for some examples for group by (also here on Stack Overflow) and check out the different results.
The other function you are searching for is called order by.
Please read some book or tutorials about sql, this is pretty basic stuff.

MySQL query to return unique values in one column and sorted by id to get a PHP array

I have the following simplified table: (Note we skipped 2nd exam in the exam_id)
+----+---------+-------+
| id | exam_id | score |
+----+---------+-------+
| 1 | 1 | 15 |
| 2 | 1 | 20 |
| 3 | 1 | 68 |
| 4 | 3 | 92 |
| 5 | 3 | 10 |
+----+---------+-------+
I want to write an $sql and some php (I'm using Wordpress, and can use $wpdb) to be able to get the following:
$exam[3]=10
$exam[1]=68
Not that when there are multiple exams, we take the score entry which corresponds to the largest id
And $exam[2] is empty. In words, I'd like to save the last ever exam that the user attempted and show their score.
I've tried using Group By and
Try this
SELECT
*
FROM exams
WHERE exams.id IN (
SELECT
MAX(id)
FROM exams
GROUP BY exam_id
);

Table Structure for alternative to storing an array in a single MySQL field

I am currently building a car booking system using PDO PHP and MySQL but am struggling with how to structure my tables for any car extras the user may have added to their booking.
I currently have the following tables at the moment which use the reservation_table to link the booking_id with the car_id (taken from their separate tables):
car_table:
ID | car_make | car_name
1 | Ford | Focus
2 | BMW | Z3
3 | Audi | A5
booking_table:
booking_ID | booking_time | total_cost | etc..
125674 | 2013-02-02 | 91.55
887463 | 2013-01-19 | 52.00
209930 | 2013-01-11 | 23.99
reservation_table:
ID | booking_ID | car_ID
1 | 125674 | 2
2 | 887463 | 2
3 | 209930 | 1
extras_table:
ID | car_extra | extra_price
1 | GPS | 22.99
2 | Baby Seat | 12.99
3 | Car Charger | 15.99
Originally I was going to add an extra column into the reservation_table using an array of the selected car extras to link them with the booking as so:
booking_table:
ID | booking_ID | car_ID | car_extras
1 | 125674 | 2 | [2,3]
2 | 887463 | 2 | [1]
3 | 209930 | 1 | [1,3]
but I have read that this is bad practice. How can I structure my tables so that I can assign the selected car extra ID's (which can range from the user selecting 0 to 12) to a particular booking?
Would the following work by creating a new table that linked just the booking_ID with the extra_ID (this would mean multiple rows of the same booking id though):
selected_extras_table:
ID | booking_ID | car_extra_ID
1 | 125674 | 2
2 | 125674 | 3
3 | 887463 | 1
4 | 209930 | 1
5 | 209930 | 3
You are exactly right: creating the selected_extras_table with a row per extra is the best solution. When you query that table, you'll get a list of all the extras in whichever booking_ID you're interested in, and you won't need to parse out multiple values from a single field (e.g., "[2,3]"). Let the database keep the values separate. You're on the right track.

How to find most common words in a MySQL database table column

i have a table in following format:
id | title
---+----------------------------
1 | php jobs, usa
3 | usa, php, jobs
4 | ca, mysql developer
5 | developer
i want to get the most popular keywords in title field, please guide.
If you have a list of keywords, you can do the following:
select kw.keyword, count(*)
from t cross join
keywords kw
on concat(', ', t.title, ',') like concat(', ', kw.keyword, ',')
As others have mentioned, though, you have a non-relational database design. The keywords in the title should be stored in separate rows, rather than as a comma separated list.
If your data is small (a few hundred thousand rows or less), you can put it into Excel, use the text-to-columns function, rearrange the keywords, and create a new, better table in the database.
SELECT title 1, COUNT(*) FROM table GROUP BY title 1
EDIT
Since you've edited and presented a non-normalized table, I would recommend you normalize it.
Have a read of: http://blog.fedecarg.com/2009/02/22/mysql-split-string-function/
You need to modify your database. You should have something like this:
items
+----+---------------+
| id | title |
+----+---------------+
| 1 | something |
| 3 | another thing |
| 4 | yet another |
| 5 | one last one |
+----+---------------+
keywords
+----+-----------------+
| id | keyword |
+----+-----------------+
| 1 | php jobs |
| 2 | usa |
| 3 | php |
| 4 | jobs |
| 5 | ca |
| 6 | mysql developer |
| 7 | developer |
+----+-----------------+
items_to_keywords
+---------+------------+
| item_id | keyword_id |
+---------+------------+
| 1 | 1 |
| 1 | 2 |
| 3 | 2 |
| 3 | 3 |
| 3 | 4 |
| 4 | 5 |
| 4 | 6 |
| 5 | 7 |
+---------+------------+
Do you see the advantage? The ability to make relations is what you should be leveraging here.

Categories