i have a little problem with a MySQL query, this is my MySQL database structure:
table user
hid(PK)
table request
id (PK), hid, word, time
table click
id, rid (FK),time
i should get the average time between the request and its related click... i've tried this but i'm not sure if it's working properly:
SELECT TIMESTAMPDIFF(SECOND,r.time,c.time)
FROM requests r RIGHT JOIN clicks c ON r.id=c.rid
WHERE r.hid='$hid'
Could someone please tell me if I'm doing it right and explain me why I'm not? I'm getting some numbers as query results, but they don't seem to be right
If you want to get the average time, you forgot to use it in your query (which looks fine besides forgetting "averaging" results).
Try this:
SELECT AVG(TIMESTAMPDIFF(SECOND,r.time,c.time)) as avg_secs_diff
FROM requests r RIGHT JOIN clicks c ON r.id=c.rid
WHERE r.hid='$hid'
GROUP BY r.hid
Related
I'm trying to get the value of id from another table
I have a table world_match :
and teams_world:
I'm trying to get the id, date, the name of the home team and away team :
Expected:
id: 1
Date: 25/12/2022
Home: Qatar
Away: Ecuador
So currently, I have a problem with my sql:
SELECT id_match, date_debut, id_domicile, id_exterieur FROM match_world m INNER JOIN teams_world t ON m.id_domicile = t.id_equipe AND m.id_exterieur = t.id_equipe
Someone can explain me my problem in this sql request please ?
I can see what you want to accomplish, but you're going about it the wrong way. You need to join the match_world twice with the teams_world table, once for the home team and once of the away team.
SELECT
match_world.id_match,
match_world.date_debut,
team_home.nom AS home_nom,
team_away.nom AS away_nom
FROM
match_world
INNER JOIN
teams_world AS team_home
ON match_world.id_domicile = team_home.id_equipe
INNER JOIN
teams_world AS team_away
ON match_world.id_exterieur = team_away.id_equipe
You were quite close. Note also that I like to write my queries in a way that they are easy to read, on several line, with indentations, and no abbreviations. This does not impact performance.
(query is untested, no guarantee of functionality given...)
I have two tables-
1) ****Company_Form****
[Contract_No#,Software_Name,Company_Name,Vendor_Code]
2) ****User_Form****
[Contract_No,Invoice_No#,Invoice_Date,Invoice_Amount,Invoice_Submit_Date]
Fields denoted with # and bold are primary keys.
=>The user has to enter a software name for which he wants to get the data of.
=>I have to structure a query in which I have to display the result in the following form:
[Contract#,Software_Name,Company_Name,Invoice_No,Invoice_Date,Invoice_Submission_Date]
Now,
one Contract_No can contain many Invoice_no under its name in
the User Form table.
One Contract_No can occur one time only in
Company_Form table
The retrieved records have to be group by the latest Invoice_Date
I came to the logic that:
I have to first retrieve all the contract numbers with that software
name from Company_Form table.
I have to query that contract number from User_Form table and display
the data for each matched contract no. fetched from Company_Form
table.
The problem is that I am unable to structure a query in SQL that can do the task for me.
Kindly help me in formulating the query.
[PS] I am using SQL with PHP.
I tried a query like:
I tried one approach as :
SELECT a.ContractNo,a.SoftwareName,a.CompanyName,b.InvoiceNo,b.InvoiceDate,b.InvAmount,b.InvoiceSubmitDate
FROM Company_Form as a,User_Form as b
WHERE b.ContractNo IN(SELECT ContractNo FROM Company_Form WHERE
SoftwareName='$Sname') AND a.ContractNo=b.ContractNo;
But I am getting a error that sub query returns more than 1 row.
Can I get help from this?
I am assuming you are attempting to find the most recent price of the user selected software and its corresponding invoice. Here is an approach to do this. If this is tested to your satisfaction, I can add necessary explanation.
select uf.Contract_No#,
cf.Software_Name,
cf.Company_Name,
uf.Invoice_No#,
uf.Invoice_Date,
uf.Invoice_Amount,
uf.Invoice_Submit_Date
from User_Form uf
inner join (
-- Most recent sale of software
select Contract_No#, max(Invoice_Date)
from User_Form
group by Contract_No#
) latest
on (
-- Filter via join for latest match records
uf.Contract_No# = latest.Contract_No#
and uf.Invoice_Date = latest.Invoice_Date
)
inner join Company_Form cf
on cf.Contract_No# = uf.Contract_No#
where cf.Software_name = :software_name
If the requirement allows your sub query to return more than one row, I would suggest you to use IN instead of = in the where clause of your main query.
Please note that I have just looked at the query and have not fully understood the requirements.
Thanks.
I worked around for some time and finally came to the following query which works like a charm
SELECT a.ContractNo,a.SoftwareName,a.CompanyName,b.InvoiceNo,b.InvoiceDate,b.InvAmount,b.ISD
FROM Company_Form as a,User_Form as b
WHERE b.ContractNo IN (SELECT ContractNo FROM Company_Form WHERE SoftwareName='$Sname')
AND a.ContractNo=b.ContractNo;
If anybody needs help in understanding the logic of this query,feel free to comment below.
I’m using Ajax to retrieve information from my database. I have no trouble sending to server PHP and getting information back using simple SQL queries. I came across a section that I needed to pull from 2 tables (Same Database) Both in common are column PO. Attached is a picture of an example.
I have been trying to pull everything from one table that meets my condition like Month of, year of and Store. I have been able to JOIN them but not successfully. Closest I ever got is it checks both tables and only returns the data that have matching PO and not the rest of Table 1.
I like for it to retrieve all rows in table one that meet the conditions and if there is a matching PO in table2 join it else continue to retrieve from table1.
Please any help would be grateful
I figured it out. Thank you
$sql = "SELECT * FROM weekly_report LEFT JOIN tracker ON weekly_report.PO = tracker.POt WHERE MONTH(STR_TO_DATE(`Need By Date (Date)`, '%m-%d-%Y')) = '$month' AND YEAR(STR_TO_DATE(`Need By Date (Date)`, '%m-%d-%Y')) = '$year' AND weekly_report.`Ship To Location (Location ID)` LIKE '%$store%' GROUP BY (weekly_report.PO)";
I'm building a sales system for a company, but I'm stuck with the following issue.
Every day I load .XML productfeed into a database called items. The rows in the productfeed are never in the same order, so sometimes the row with Referentie = 380083 is at the very top, and the other day that very same row is at the very bottum.
I also have to get all the instock value, but when I run the following query
SELECT `instock` FROM SomeTable WHERE `id` > 0
I get all values, but not in the same order as in the other table.
So I have to get the instock value of all rows where referentie in table A is the same as it is in table B.
I already have this query:
select * from `16-11-23 wed 09:37` where `referentie` LIKE '4210310AS'
and this query does the right job, but I have like 500 rows in the table.
So I need to find a way to automate the: LIKE '4210310AS' bit, so it selects all 500 values in one go.
Can somebody tell me how that can be done?
I'm not even sure I understand your problem...
Don't take this personally, but you seem to be concerned/confused by the ordering of the data in the tables which suggests to me your understanding of relational databases and SQL is lacking. I suggest you brush up on the basics.
Can't you just use the following query?
SELECT a.referentie
, b.instock
FROM tableA a
, tableB b
WHERE b.referentie = a.referentie
I'm using PHP and under localhost using Wamp. I have 2 tables from my database which has a huge data almost 13,000 plus each table. I want to check if NameFromA from TableA exist in NameFromB from TableB. I have this code working when I try to use small amount of data around 100 data.
SELECT * FROM TableA WHERE EXISTS (SELECT * FROM TableB WHERE NameFromA = NameFromB)
My problem is when I try running it and comparing the 13,000 plus data nothing happens. It has no output.
Create index on column NameFromA in TableA and NameFromB in TableB.
Try below query:
SELECT a.*
FROM TableA AS a, TableB AS b
WHERE a.NameFromA = b.NameFromB
Try this:
SELECT
TableA.*
FROM
TableA
INNER JOIN TableB ON (TableA.NameFromA = TableB.NameFromB)
If it is still slow, may be you have problem in the DB or the timeout time is too short.
You can also try to run this in MySql management too to see how long that query will take.
One more thing. The data that you retreive must be send to web server. If your table contains a lot of data for each row and there are a lot of rows this also will take time.
EDIT:
Ok #mar. You have to make a little troubleshooting to find where is the problem.
First: if your PC is powerfull enought. Because for normal PC and Server 13k records are nothing.
Second: are you sure that there are at least one name from table A which is presented in table B?
Third: try to run query in external SQL tool - not in php. If it retunrs correct set quickly, then the problem is in php.