Generate sort by list - php

I have a data like this :
car_id | price_id | transmission_id | group_id | image_id
=======|==========|=================|==========|=============
1. CAR A | $20/hour | Automatic | GROUP A | image_1.jpg
2. CAR B | $30/hour | Automatic | GROUP B | image_2.jpg
3. CAR C | $40/hour | Manual | GROUP C | image_3.jpg
4. CAR D | $20/hour | Manual | GROUP A | image_4.jpg
5. CAR E | $30/hour | Manual | GROUP B | image_5.jpg
6. CAR F | $40/hour | Automatic | GROUP D | image_6.jpg
So, the lists can be selected (choose) by "group_id" or "transmission_id".
User can tick and automatically generate result sort by :
Group A,B,C,D
Transmission Automatic,Manual
Group + Transmission
The result shown on the same page. Is it possible if I don't want to use MySQL?

Related

Return all wows, mached and unmatched in MySQL Query

I have two tables and i'm like to query 2 tables to obtain a report.
POSITION
+-------------+---------------+
| position_id | position_name |
+-------------+---------------+
| 1 | E1P1 |
| 2 | E1P2 |
| 3 | E3P3 |
| 4 | E4P4 |
+-------------+---------------+
PEOPLE
+------------+-------------+--------------------+
| people_id | people_name | people_position_id |
+------------+-------------+--------------------+
| 1 | JOHN | 2 |
| 2 | MARK | 4 |
+------------+-------------+--------------------+
QUERY
SELECT position_id, position_name, people_name FROM position
RIGHT JOIN people ON people_position_id = position_id
When I use simple query I get only matched rows, ho to obtain all?
I'm like to obtain this result
+----+----------+--------+
| ID | POSITION | STATUS |
+----+----------+--------+
| 1 | E1P1 | Empty |
| 2 | E1P2 | JOHN |
| 3 | E3P3 | Empty |
| 4 | E4P4 | MARK |
+----+----------+--------+
I would use a left join here:
SELECT
po.position_id,
po.position_name,
COALESCE(pe.people_name, 'EMPTY') AS STATUS
FROM position po
LEFT JOIN people pe
ON po.position_id = pe.people_position_id;
By the way, the reason your current right join attempt is failing is that you have placed the people table on the right side of the join. This means that non matching position records would be discarded. Here is my answer above, rewritten using a right join:
SELECT
po.position_id,
po.position_name,
COALESCE(pe.people_name, 'EMPTY') AS STATUS
FROM people pe
RIGHT JOIN position po
ON po.position_id = pe.people_position_id;
Note carefully that the table order has switched. Most of the time, you will see people using left joins rather than right joins.

Select all fields from a table where a field in another table with an ID is equal to a string

I have 2 tables:
Ads: Fields ID, A, B and C:
+----+---+-------+------+
| ID | A | B | C |
+----+---+-------+------+
| 1 | x | y | z |
| 2 | c | v | b |
| 3 | n | n | m |
+----+---+-------+------+
Requests: Fields ID, AdID, and Status:
+----+------+----------+
| ID | AdID | Status |
+----+------+----------+
| 3 | 1 | pending |
| 4 | 2 | approved |
| 5 | 3 | pending |
+----+------+----------+
ID (from Ads) = AdID (from Requests).
Now, I want to get all records from Ads where AdID's (from Requests) Status equals pending. AdId here would be the value ID from Ads.
So, with the above tables, the result I'd get would be ID 1 and 3 from Ads:
+----+---+---+---+
| ID | A | B | C |
+----+---+---+---+
| 1 | x | y | z |
| 3 | n | n | m |
+----+---+---+---+
This is the closest I've got so far, but this obviously doesn't work because it can only select one row - whereas I need to select many:
SELECT * FROM Ads WHERE ID = (SELECT AdID FROM Requests WHERE Status = 'pending')
This might not make sense - please ask if I haven't explained it well - I'll help as much as possible :)
Use IN in place of =:
SELECT *
FROM Ads
WHERE ID IN (SELECT AdID FROM Requests WHERE Status = 'pending')
Try this:
SELECT * FROM Ads A LEFT JOIN Requests R ON A.ID=R.AdID WHERE R.AdID='pending';

query specific table based on search keyword in mysql

I am creating a search portal in PHP from which user can search for a specific cuisine. In MySQL I have multiple tables for each cuisine and the respective hotel names that offer the cuisine. For example, in table
How can I query a specific cuisine table based on the cuisine search keyword?
So if a user enters 'mexican' as the search query, how can it connect to the 'Table2 - Mexican' and return the hotel names from this table?
Table1 - Chinese
_______________________
| id | hotelname |
|______|______________|
| 1 | hotel1 |
| 2 | hotel2 |
| 3 | hotel3 |
| 4 | hotel4 |
| 5 | hotel5 |
|______|______________|
Table2 - Mexican
_______________________
| id | hotelname |
|______|______________|
| 1 | hotel1 |
| 2 | hotel2 |
| 3 | hotel3 |
| 4 | hotel4 |
| 5 | hotel5 |
|______|______________|
Table3 - Pizza
_______________________
| id | hotelname |
|______|______________|
| 1 | hotel1 |
| 2 | hotel2 |
| 3 | hotel3 |
| 4 | hotel4 |
| 5 | hotel5 |
|______|______________|
Your database concept is very unflexible. I think you should put the cuisines into your database as information (i.e. table content) instead of metadata describing single tables. Tables should generally considered to be static just like the code you write to access the database and its tables. If you implement the cuisines as different tables you would have to hardwire every cuisine into your code.
Here is a suggestion for a better approach:
Create a hotels table to store all the hotels,
Create a cuisines table to store all the different types of cuisines,
Make an additional table to establish the n:m relationship between the hotel and the cuisine.
Example:
hotels: id, name, address, city, telno, email
cuisine: id, name, description
rel: cuisine, hotel (where both are the foreign keys to the
id columns of the respective tables above)
See also:
How to handle a Many-to-Many relationship with PHP and MySQL.
MySQL: Many To Many Relationships ยป Return True
You might want to check this question to create a many-to-many relationship:
many-to-many and many-to-many intersections
I guess what you would like to achieve is something like this:
Table1 - Hotel
_______________________
| id | hotelname |
|______|______________|
| 1 | hotel1 |
| 2 | hotel2 |
| 3 | hotel3 |
| 4 | hotel4 |
| 5 | hotel5 |
|______|______________|
Table2 - Cuisine
____________________________________________
| id | cuisine_name | keywords |
|______|______________|____________________|
| 1 | Chinese | Shandong,Noodles,. |
| 2 | Mexican | Tacos,Beans,... |
| 3 | Itarian | Pizza,Pasta,.. |
|______|______________|____________________|
Table3 - HotelCuisine
___________________________________
| id | hotel_id | cuisine_id |
|______|____________|______________
| 1 | 1 | 2 |
| 2 | 1 | 3 |
| 3 | 2 | 1 |
| 4 | 2 | 2 |
| 5 | 3 | 3 |
|______|____________|_____________|
SQL:
SELECT hotelname, cuisine_name FROM Hotel
INNER JOIN HotelCuisine ON Hotel.id = HotelCuisine.hotel_id
INNER JOIN Cuisine ON Cuisine.id = HotelCuisine.cuisine_id
WHERE keywords like '%pizza%'
Result:
________________________________________
| hotelname | cuisine_name |
|_______________|______________________|
| hotel1 | Itarian |
| hotel3 | Itarian |
|_______________|______________________|
DEMO: http://sqlfiddle.com/#!2/961de/1
Hope this helps
you can check SQL UNION. But instead of having multiple tables with the same fields, you can try normalization to minimize the redundancy and to make queries easier.
Something like:
Hotel Table
-----------------------------
id | hotelname | categoryID
------------------------------
1 | hotel name 1 | 1
2 | hotel name 2 | 2
-----------------------------
Category Table
-------------------
id | categoryname
-------------------
1 | chinese
2 | mexican
------------------
And query as simple as:
SELECT a.hotelname, b,categoryname
FROM hotel_table a
LEFT JOIN category_table b
ON a.categoryID = b.id AND b.categoryname LIKE '%mexican%';

MySQL - insert row after sort

if i have this table:
perma_table:
+-------+-------+
| A | B |
+-------+-------+
| a | 5 |
| c | 7 |
| a | 8 |
| b | 9 |
| a | 7 |
| c | 6 |
| a | 8 |
+-------+-------+
i want to make
inserted_table:
+-------+-------+-------+
| A | B | C |
+-------+-------+-------+
| a | 5 | 1 |
| a | 7 | 2 |
| a | 8 | 3 |
| a | 8 | 4 |
| b | 9 | 5 |
| c | 6 | 6 |
| c | 7 | 7 |
+-------+-------+-------+
how to do this?
im using code igniter (php), is this all of this method can be done by using query, without using CI active record/php function?
You can do it with a single query
CREATE TABLE inserted_table as
SELECT A,B,#num:=#num+1 AS C
FROM perma_table,(select #num:=0) temp
ORDER BY A,B;
If you want just a query to do it, its so simple:
Insert it normally, after inserted you can select
SELECT * FROM table ORDER BY A, B, C ASC
But if you want to keep this order, without at any select without order you can create a view or a new table like
CREATE VIEW inserted_table AS
SELECT A, B, #id = #id+1 AS C FROM perma_table
ORDER BY A,B;
OR a new table (no view)
CREATE TABLE inserted_table AS
SELECT A, B, #id = #id+1 AS C FROM perma_table
ORDER BY A,B;
With view you can insert any value on perma_table, the inserted_table will automaticaly generated, if you choose the second case, you heave to recreate the table averytime when you edit perma_table

How to create MYSQL query to get specific results (count all unique values from one column)

Please look at my table's structure:
-------------
| id | name |
-------------
| 1 | a |
| 2 | a |
| 3 | a |
| 4 | b |
| 5 | b |
| 6 | c |
| 7 | d |
| 8 | d |
I would like to create a query (in PHP), to get such results (in HTML):
<p>name: a (number of letters: 3)</p>
<p>name: b (number of letters: 2)</p>
<p>name: c (number of letters: 1)</p>
<p>name: d (number of letters: 2)</p>
SELECT count(name),
name
FROM MyTable
GROUP BY name
ORDER BY name ASC;
I assumed your table is named MyTable exchange it accordingly.

Categories