sort results from database by cell value - php

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.

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')

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
);

Using Two Tables in PHP & MySQL to display one loop result

Ok so a bit of a funny question here. I have two tables: -
LiveTable
ArchiveTable
The data is as follows: -
Table: LiveTable Table: ArchiveTable
| ID | NAME | | ID | NAME |
------------------ ------------------
| 1 | Test One | | 4 | Test Four |
------------------ ------------------
| 2 | Test Two | | 5 | Test Five |
------------------ ------------------
| 3 | Test Three| | 6 | Test Six |
What I want to do is merge them into one table for querying purposes only. Not as a Database Structure.
In essence when I do a PHP Loop I want the results to work like this: -
Merged Results
| ID | NAME |
------------------
| 1 | Test One |
------------------
| 2 | Test Two |
------------------
| 3 | Test Three|
------------------
| 4 | Test Four |
------------------
| 5 | Test Five |
------------------
| 6 | Test Six |
How would I go about doing this? Also is there a way of doing this with Doctrine?
You can use an SQL query with UNION:
SELECT ID, Name
FROM LiveTable
UNION ALL
SELECT ID, Name
FROM ArchiveTable
Note: UNION ALL will retain duplicates. If you want to remove duplicate records, then use UNION.
Yould use UNION:
SELECT id, name FROM tbl1
UNION ALL
SELECT id, name FROM tbl2
Create DB view using above two tables
CREATE VIEW view_name AS SELECT
id, name FROM tbl1
UNION
id, name FROM tbl2
Then you can query on your view
you can use the sql query multi table select
Select * from LiveTable,ArchiveTable
that's it

Mysql join query multiple values

I have a table called facility.
Structure looks as follows:
id | name
---------
1 | Hotel
2 | Hospital
3 | medical shop
I have an other table which is taking data from the above table and keeping multiple values in one column. View looks like below:
id | facilities
---------------
1 | Hospital~~medical shop~~Hotel
2 | Hospital~~Hotel
3 | medical shop~~Hotel
If I want to join these two tables how does the query look like?
I tried this, but it didn't work:
select overview.facilities as facility
from overview join facility on facility.id=overview.facilities;
you can do this with a bit of hackery
select o.facilities as facility
from overview o
join facility f on find_in_set(f.facilities, replace(o.facilities, '~~', ','));
I would highly recommend you change the way you are storing data. currently it is considered un normalized and that quickly becomes a monster to deal with
you should change your table structure to look something more like this
+----------+--------------+
| facility |
+----------+--------------+
| id | name |
+----------+--------------+
| 1 | Hotel |
| 2 | Hospital |
| 3 | medical shop |
+----------+--------------+
+-----------+-------------+
| overview |
+-----------+-------------+
| id | facility_id |
+-----------+-------------+
| 1 | 2 |
| 2 | 3 |
| 3 | 1 |
| 4 | 2 |
| 5 | 1 |
| 6 | 3 |
| 7 | 1 |
+-----------+-------------+
Code Explanation:
basically you are wanting to find the matching facilities in the overview. one handy function MySQL has is FIND_IN_SET() that allows you to find an item in a comma separated string aka find_in_set(25, '11,23,25,26) would return true and that matching row would be returned... you are separating your facilities with the delimiter ~~ which wont work with find_in_set... so I used REPLACE() to change the ~~ to a comma and then used that in the JOIN condition. you can go from here in multiple ways.. for instance lets say you want the facility id's for the overview.. you just add in the select GROUP_CONCAT(f.id) and you have all of the id's... note if you do that you need to add a GROUP BY at the end of your query to tell it how you want the results grouped

Mysql - find fields with same id

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

Categories