MySQL - How do I order based on columns from another table - php

I have these tables:
Table 'Data':
| Customer | Category | Brand | Market | A | B |
|==========|==========|=======|========|===|===|
| Bill | 5 | 4 |1 |2 |9 |
| Bill | 5 | 4 |1 |6 |15 |
| Bill | 5 | 4 |1 |1 |30 |
| Greg | 7 | 9 |3 |5 |1 |
| Amy | 9 | 2 |1 |1 |8 |
Table 'Customer':
| Cust | Mkt | SubMkt |
|======|=====|========|
| Bill | 1 | NY |
| Bill | 2 | Arizona|
| Bill | 3 | Cali |
| Greg | 1 | Ohio |
| Amy | 1 | Texas |
Table 'Sort':
| SubMarket | SortBy |
|===========|========|
| Cali | A |
| Ohio | B |
| Arizona | A |
| NY | A |
I need to select all from 'Data' ordered by a few different columns and then either A or B depending on what 2 other tables say.
SELECT *
FROM Data
ORDER BY Customer, Category, Brand, Market, (A or B)
Basically, Customer and Market in table Data links to Cust and Mkt in table Customer, and then SubMkt in table Customer links to SubMarket in table Sort. Then SortBy in table Sort would be the column I need to sort by in table Data. Hope that makes sense.
So, in the above example, all of the rows in Data containing both "Bill" for Customer and "1" for Market would be sorted by column A.
I started to write an array of objects in PHP and then I would sort the array based on an object property but this would actually require a huge overhaul on my existing code. I assume what I'm looking for can be done with a fairly straight forward query in MySQL but I don't know MySQL well enough to write that query. Any help would be appreciated!
Edit: I should have mentioned, these are just partial tables. The actual tables I'm working with are thousands of rows and every "SubMarket" does have a matching "SortBy".
Edit 2: Clarified that Customer and Market is needed in table Data to link to Cust and Mkt in table Customer.

this is just an answer showing how to do the joins.. Ignacio should have the accepted answer as he provided the hard part first :)
SELECT d.*
FROM data d
JOIN customers c on c.cust = d.customer AND c.mkt = d.market
JOIN sort s on s.submarket = c.submkt
ORDER BY
d.Customer,
d.Category,
d.Brand,
d.Market,
CASE s.sortby
WHEN 'A' THEN d.A
WHEN 'B' THEN d.B
ELSE 9999999
END

You need a CASE expression.
CASE `Sort`.`SortBy`
WHEN "A" THEN `Data`.`A`
WHEN "B" THEN `Data`.`B`
ELSE NULL
END CASE
The ELSE NULL part is optional, but it can help reveal data problems.

Related

MySQL select row from one table with multiple rows in a second table

I have one table containing "Client" information, and another including "Payment" information for each client.
Simplified client table
client_id | client_name | client_status
----------+----------------+--------------
1 | John St. Peter | In Arrears
2 | Peter St. John | Up-to-date
Simplified payments table
payment_id | client_id | payment_date | payment_amount
-----------+-----------+--------------+---------------
1 | 1 | 2017-12-12 | 123.45
2 | 2 | 2017-12-15 | 234.56
3 | 1 | 2017-12-17 | 23.45
4 | 1 | 2017-12-21 | 54.32
5 | 2 | 2017-12-23 | 34.56
With the above two tables, I want to produce a single table with a single query with all the pertinent information to generate a search grid where I can filter by any column in either of the two tables, namely, filter by "client_status" from "client" table, or "client_id" from "payments" table.
payment_id | client_id | client_name | client_status | payment_date | payment_amount
-----------+-----------+----------------+---------------+--------------+---------------
1 | 1 | John St. Peter | In Arrears | 2017-12-12 | 123.45
2 | 2 | Peter St. John | Up-to-date | 2017-12-15 | 234.56
3 | 1 | John St. Peter | In Arrears | 2017-12-17 | 23.45
4 | 1 | John St. Peter | In Arrears | 2017-12-21 | 54.32
5 | 2 | Peter St. John | Up-to-date | 2017-12-23 | 34.56
So - in essence, I want to "duplicate records" in the clients table for as many times as necessary for corresponding records in the payments table to facilitate the search. I am using a premade grid tool (DataTables) which is used all over the system with a custom function made by the developer to generate the grid (so I don't want to mess with that function in fear of breaking the rest of the system), so a single query to get that data sounds like the more pragmatic approach. I also have an "Edit" feature for each row, which will edit the "client" table, and put a tabbed structure in the edit screen for the "payments" table, which currently works fine with two queries (one for the client, one getting all the payments for that client).
I have tried UNION as well as various JOIN statements (probably incorrect...), but either get syntax errors or a single result per "client" row, meaning it does not pick up in the filters.
Apologies if this is a duplicate question - I have searched, but could not find an answer that answers this scenario for me.
here is a join...
select *
from clientsTable c,
paymentsTable p
where c.client_id = p.client_id
order by p.payment_id;
that should give you everything.
edit: for empties... payments with no clients...
select *
from clientsTable c,
paymentsTable p
where c.client_id = p.client_id
or p.client_id = null
order by p.payment_id;

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

Database table structure query

Just a quick query on the best databased structure to go with for a small application, [ the database will be SqlLite or MySql and the app written in PHP if that helps]
Basically my issue is that entries will have many options and these will be likely to be added to and removed so editing the db structure is probably not the best idea. I am just wondering what the best way to store this options to the data base. In similar situations in the past I have achieved this by storing the data serialised or in JSON format to a table row, but I am wondering if this is the best option, any advice would be great.
The application is map based where a user can add/remove markers, the markers have types [ eg shop, school, hospital etc. ] the user can also add and remove types. In addition to this the use can create map views eg a map where only the school and hospitals are visible.
It is a bit hard to describe but here is a go describing the [ simplified ] table structure
markers
+---------+-----+-----+--------+-----------+
|markerID | lat | lng | typeID | name |
+---------+-----+-----+--------+-----------+
|1 | 52 | -9 | 1 | A School |
|2 | 52 | -9 | 2 | A Shop |
|3 | 52 | -9 | 1 | B School |
|4 | 52 | -9 | 3 | A Hospital|
+---------+-----+-----+--------+-----------+
marker types
+-------+-------------+--------------+
|typeID | name | icon |
+-------+-------------+--------------+
|1 | Schools | school.png |
|2 | Shops | shop.png |
|3 | Hospitals | hospitals.png|
+-------+-------------+--------------+
map view
+------+---------------------+---+
|mapID | name | ??|
+------+---------------------+---+
|1 | Schoool & Shops | ??|
|2 | Hospitals & Schools | ??|
+------+---------------------+---+
So my question is basically, what is the best way to store the information that mapID #42 should display markers with typeID #2, #5, and #23 for example.
As i understand, the Map view could have many markers ( which is logic). so i think a join table would be the best solution. A table for example called MapMarkers that has the following structure :
MapMarkers
| id | MapID | MarkersID | ?? | ( you can add other infos for example if you want to store personnalized maps for each client you can add userID)
You should use a third table which represents a relation between two tables:
mapView ---- < mapView-markerType > ---- markerType
mapView-markerType would have the attributes: | id | mapID | typeID |
(you should get a shorter name than mapView-markerType, obviously)
EDIT
Considering the changes you made on the post, you would get:
mapView_markerType
id* | mapId | typeId
--------|----------|----------
1 | 1 | 1
2 | 1 | 2
3 | 2 | 1
4 | 2 | 3
EDIT2
So, to further explain what I meant on the comment below:
map view
+------+---------------------+----------+
|mapID | name | typeComb |
+------+---------------------+----------+
|1 | Schoool & Shops | {1,2} |
|2 | Hospitals & Schools | {1,3} |
+------+---------------------+----------+

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.

Displaying Data From Multiple MySQL Tables

I have 14 tables (one for every year) with product code, firm name and invoice numbers. Main structure of table is identical (product code, ID), but there can be some variables in names of firms.
Table2011
| ID | productcode | firm1 | firm2 | firm3 | etc |
| 1 | G-00001 | 2;5;40| 32;67 | | 150 |
| 2 | G-00005 | | 50 | | |
|etc | | | | | |
Table2010
| ID | productcode | firm1 | firm2 | firm3 |etc |
| 1 | G-00001 | 1;10 | | 55 | |
| 2 | G-00003 | | 2 | | |
| 3 | G-00005 | | 50 | 40 | |
| etc| | | | | |
Table2009
...
Column Firm1 do not usually equals to same firm as firm 1 in other table
I am using table editor to work with tables (adding columns to table, editing values…).
I would like to know if it is possible to achieve result like below. It is above my PHP skills.
Product G-00001 page
…
<UL>
<LI>Year 2011: 150etc; 67firm2; 40firm1; 32firm2; 5firm1; 2firm1</LI>
<LI>Year 2010: 55firm3; 10firm1; 1firm1</LI>
<LI>Year 2009: ...</LI>
...
</UL>
…
Lemme begin with book recommendation : SQL Antipatterns. You will need it, doesn't matter if you caused this mess or ar just assigned to fix it.
If i was in your place, first thing would do would be to fix the database structure. This is madness. You do not need a new table for each year and new column for each company. Database is not a form of Excel spreadsheet.
Invoices Years Companies
----------------- ------------- ---------------
| product_code PK | | year_id PK | | company_id PK |
| company_id FK | | number | | title |
| amount | ------------- ---------------
| year_id FK |
-----------------
Where PK - primary key and FK - foreign key.
This structure would make the gathering of information much much much MUCH easier.
If you just want to display the data and not worry about the restructuring just yet you can use a JOIN to display the information from all the tables.
Although I would agree with teresko you really need to redesign that database. It is not maintainable the way it is.

Categories