mysql join table1 to last entry of table2 - mysql

I have a simple mysql query that get customer's equipment detail (general info like id, name, job site):
SELECT *
FROM equipments
WHERE CUSTOMER_ID = 87
But each equipment has is own table of variable datas like fuel level, engine hours, latitude, longitude (the equipment ID is the name of the table). So now, I'm making a query that takes all the general info and put it into an array. After that, I create a loop that include a query for each equipment who looks into the right table (table name = equipement ID) and get the last entry.
I tested it with 4 equipments and it takes on average 4 seconds to load the page which is a bit too long. Is there a way to make it shorter and put this inside one query?
Can I join the first table with the last entry of the second table? The second table is changing and named like the equipment id of the first table.
I found this post, but I can't make it work for this application because I can't set the ON statement.
https://stackoverflow.com/a/3619209/1895428

Related

Advanced Search Functionality - SQL

i'm working on an advanced search functionality on my website.
Basically data I'm working on is stored within two tables.
First table contains basic information about the product (1 row = 1 product so it's unique).
Table structure may look like this:
id, title, description
The second table contains more information about the product. The product may but don't have to have any rows here. However, one product may store in the second table a few rows. What's more - data in the second table should be used to the advanced search functionality. Table structure may looks like this:
id, item_id (from table 1), value_id (from another table), value
I want to select only these products (table 1) which has specified value_id (from column 2):
... WHERE table1.item_id = 5 AND table2.value_id = 1000
As I mentioned before - table 2 may but doesn't have to contains any rows connected by item_id with the table 1.
I've tried to use JOIN/LEFT JOIN function in SQL but in this case when the product has 3 rows in the table 2 - a query result returns 3 rows instead of 1 or 0 (if not found any results).
How can I handle that?
You want to select products. So select from the product table. You want to select only those for which exists a certain attribute. So create an approriate WHERE clause. As you want to look up data in another table, you could use EXISTS or IN.
select *
from items
where id in (select item_id from item_values where value_id = 1000);

mysql query using column values as parameter in query phpMyAdmin

I have a query i have been working on trying to get a specific set of data, join the comments in duplicate phone numbers of said data, then join separate tables based on a common field "entry_id" which also happens to be the number on the end of the word custom_ to pull up that table.
table named list and tables containing the values i want to join is custom_entry_id (with entry_id being a field in list in which i need the values of each record to replace the words in order to pull up that specific table) i need entry_id from the beginning part of my query to stick onto the end of the word custom for every value my search returns to get the fields from that custom table designated for that record. so it will have to do some sort of loop i guess? sorry like i said I am at a loss at this point
this is where i am so far:
SELECT * ,
group_concat(comments SEPARATOR '\r\n\r\n') AS comments_combined
FROM list WHERE `status` IN ("SALEA","SALE")
GROUP BY phone_number
//entry_id is included in the * as well as status
// group concat combines the comments if numbers are same
i have also experimented on test data with doing a full outer join which doesnt really exist. i feel if you can solve the other part for me i can do the joining of the data with a query similar to this.
SELECT * FROM test
LEFT JOIN custom_sally ON test.num = custom_sally.num
UNION
SELECT * FROM test
RIGHT JOIN custom_sally ON test.num = custom_sally.num
i would like all of this to appear with every field from my list table in addition to all the fields in the custom_'entry_id' tables for each specific record. I am ok with values being null for records that have different custom fields. so if record 1 has custom fields after the join of hats and trousers and record 2 has socks and shoes i realize that socks and shoes for record 1 will be null and hats and trousers for record 2 will be null.
i am doing all this in phpmyadmin under the SQL tab.
if that is a mistake please advise as well. i am using it because ive only been working with SQl for a few months. from what i read its the rookie tool.
i might be going about this all wrong if so please advise
an example
i query list with my query i get 20,000 rows with columns like status, phone_number, comments, entry_id, name, address, so on.
now i want to join this query with custom fields in another table.
the problem is the custom tables' names are all linked to the entry_id.
so if entry_id is 777 then the custom table fields are custom_777
my database has over 100 custom tables with specials fields for each record depending on its entry_id.
when i query the records I don't know how to join the custom fields that are entry_id specific to the rest of my data.i will pull up some tables and data for a better example
this is the list table:
this is the custom_"entry_id"
Full Outer Join in MySQL
for info on full outer joins.

SQL Query/Procedure with user input to view all persons meeting a criteria

I have an SQL database which I need to create a procedure or query that is linked to a web front end where the user can type in a value which will be in one of the columns of the database. The user types in the value, submits the command (just normal text entry in a textbox in the website), a query is ran in the background to view all persons who have this value in the column under their entry.
User entry to determine the outcome for a view of specific people.
SQL Side I have currently but not certain it is correct, works but not as intended!
I have a table with people in it and a model of interest value column.
Each model of interest is just a number 1-7 which relates to a product range in another table. So 1 = a Corsa, 2 = Astra, 3 = Vectra etc.
So in the persons table, they will have purely a 1 or a 2 etc. Now I need it to be linked to the model table so if it sees 1 it looks for Corsa rather than the number 1.
A user types in the frontend, select all users interested in a Corsa, it will look up the word corsa and match it to the value 1, then search for 1 in the model of interest column in the person table.
So far I have the following query. Any suggestions as I'm stumped for today. Will try again tomorrow.
SELECT TOP 100 [ld_idno]
,[ld_company]
,[ld_decisionmaker]
,[ld_decisionmaker_workphone]
,[ld_decisionmaker_mobile]
,[ld_decisionmaker_email]
,[ld_discussion_model]
FROM [FMLive204].[dbo].[tblLeads]
SELECT a.po_word
FROM dbo.tblPopulation a
INNER JOIN dbo.tblLeads b
ON ld_discussion_model = po_idno
WHERE [po_word]='Corsa'
SELECT TOP 100 Unique Records
company name
customer name
customer workphone]
customer mobile]
customer email]
[ld_discussion_model] Model of interest = 1
FROM [FMLive204].[dbo].[tblLeads]
SELECT a.po_word
FROM dbo.tblPopulation a (table a)
INNER JOIN dbo.tblLeads b (table b)
ON ld_discussion_model = po_idno (if the model of interest 1 is the same as an entry in the other table, pick the value in the next columne (po_word)
WHERE [po_word]='Corsa'
Daniel,
Below I've outlined the stored procedure which would require the userdata (e.g. Corsa entered from the website) and return all fields from tblpopulation where 'po_idno' is in the list matched against the tblLeads. Not having the tblpopulation definition I'm guessing for field name 'po_idno' is the field which has the value 1-7 in it.
Create PROCEDURE [dbo].[sp_find_person] (#UserData varchar(6))
AS
BEGIN
SELECT Top 100 * FROM [FMLive204].[dbo].tblpopulation
where po_idno in (SELECT po_idno FROM dbo.tblLeads b
JOIN dbo.tblPopulation a
ON ld_discussion_model = po_idno
WHERE [po_word]=#UserData)
Once you create the procedure you'll have to execute it which more information can be found # http://msdn.microsoft.com/en-us/library/ms189915.aspx

Selecting data from a table conditional on 2 unique values from another table

I've just started using SQL and had been getting by with searching google for quick tips until now, however I've been stuck on this one for a while.
I have two tables, Stock_Data has 2 columns, Id and Date. The second table is very large, containing a couple of million rows of data and is called Options_Data. This second table also has an Id column and a Date column along with around 12 extra data columns that I'd like to download.
What I would like to do is
for each row i in Stock_Data
SELECT * FROM Option_Data where Option_Data.Id = Stock_Data.Id(i)
AND WHERE Option_Data.Date BETWEEN Stock_Data.Date-60 days and Stock_data.Date+60 days
end
Essentially; grab a stock ID and it's unique event date from Stock_Data, head over to Option_Data and take out the data for that stock ID 60 days before and after the event date.
I've only ever really coded properly in matlab hence the loop analogy and pseudo code.
I can solve the problem using Id's only but it involves downloading far more data than is necessary, it's the unique range of data for each ID that has me in trouble.
SELECT
Option_Data.*
FROM
Stock_Data
INNER JOIN
Option_Data
ON Option_Data.ID = Stock_Data.ID
AND Option_Data.Date BETWEEN Stock_Data.Date-60 and Stock_data.Date+60

Multitable stores when other store locations are in sub-table

I have a database that allows a list of businesses.
The Master table has the customer aka businesses details:
id
firstname
lastname
tradingname
storeaddress
state
postcode
In a table called Otherstores, I have the following:
master_id
store_id
storeaddress
state
postcode
phonenumber
What I now need to do is a PHP script that allows me to show all the stores in a list function but here is the catch:
I only want to show 8 stores from different types of categories so they are random.
However I then need it NOT to show a store twice on the same search.
I need it to make the sub-stores aka Otherstores also be randomly added into the query so that they are seeable as well.
I wondering the best way to do this.
WHY I DON'T HAVE ANY CODE:
It's tough to show you code as my idea was to do a left join or INNER join and limit it to id 1.
However I know that won't work because I would need to be able to join them together some how, but I want each sub store to be like its a master store so if I join it to the master table I can't see that working, and instead you will just get errors.