Sql query selecting from both tables. - mysql

I have a query which looks like this
$db->query("SELECT A.page_id, A.page_name FROM user_likes_pages as A , user_likes as B WHERE A.page_id = B.page_id AND B.country_id = ".$user_reg." ");
The thing is, I want to select a column from user_likes. Do I have to make a join or I can do it in different way. Thank you.

You have a join in your query, but it is implicit. You should write the query as:
SELECT ulp.page_id, ulp.page_name, ul.<whatever>
FROM user_likes ul JOIN
user_likes_pages ulp
ON ul.page_id = ulp.page_id
WHERE ul.country_id = ".$user_reg."
In addition to adding the explicit join syntax, I also changed the table aliases so they are abbreviations of the table name. This makes it easier to read the query and avoid mistakes.

You select B.columnname. You don't need a join, because you have the A.page_id = B.page_id.

Related

Don't Know How To Join Using Calculation and a column

I do not know how to join tables based on a calculation. I have to take a substring to get the part of a string I need to match up to a column from another table. I cannot figure out how to join them and really don't know where to start.
I tried everything in my power but I literally took a beginner's class and now have to fend for myself.
Select *
From five9_data.calllog join warbird.user
ON warbird.attr_employee = substring(five9_data.calllog.agent, 4,position('#' in five9_data.calllog.agent)- 4)
Group By warbird.attr_employee
Order warbird.attr_employee
Limit 100
I tried the above in the Select command but figured out it will not work and that I need to use the calculations in the join statement, but have no idea on syntax/formula. A few examples made as simple as possible would be great. I also have issue with the Group By Order by with this.
Shown above.
Often, the join condition would look like:
from t1 join
t2
on t1.empid = concat('%', t2.agent, '%')
Or, you can just use the expression:
from t1 join
t2
on t1.empid = substring(t2.agent, 4, position('#' in t2.agent) - 4)
EDIT:
As for your example code, I would write it as:
Select b.attr_employee, . . . -- aggregation functions go here
From five9_data.calllog cl join
warbird.user u
on u.attr_employee = substring(cl.agent, 4, position('#' in cl.calllog.agent) - 4)
Group By u.attr_employee
Order u.attr_employee
Limit 100;
Here are changes to notice:
Table aliases make the query easier to write and to read.
When using GROUP BY, the only unaggregated columns in the SELECT should be the GROUP BY keys. The rest should be aggregated.
Your problem is that warbird.attr_employee is not defined, because you have missed the table name. However, u is so much easier to write and to read.
from t1 join
t2
on t1.empid = substring(t2.agent, 4, position('#' in t2.agent) - 4)

MAX(Date) is giving empty result

I have a table with exchange rate like below
And I am using the maxofdate to pick all these values based on currency code. But the query is giving blank.
Select USDAMOUNT * dbo.EXCHANGERATEAMT
from dbo.Amount_monthly
Left Join dbo.EXCHANGERATE on dbo.Amount_monthly.Currencycode=dbo.EXCHANGERATE.fromcurrencycode
WHERE ValidToDateTime = (Select MAX(ValidToDateTime) from dbo.EXCHANGERATE)
AND dbo.EXCHANGERATE.EXCHANGERATETYPECODE = 'DAY'
Using this statement
CONVERT(DATE,ValidToDateTime) = CONVERT(DATE,GETDATE()-1)
instead of subquery is giving me expected result.
Can someone correct this.
thanks in advance.
If I understand correctly, you need two things. First, the condition for the max() needs to match the condition in the outer query. Second, if you really want a left join, then conditions on the second table need to go in the on clause.
The resulting query looks like:
Select . . .
from dbo.Amount_monthly am Left Join
dbo.EXCHANGERATE er
on am.Currencycode = er.fromcurrencycode and
er.ValidToDateTime = (Select max(er2.ValidToDateTime)
from dbo.EXCHANGERATE er2
where er2.EXCHANGERATETYPECODE = 'DAY'
) and
er.EXCHANGERATETYPECODE = 'DAY';
I would write this using window functions, but that is a separate issue.
Try removing WHERE clause for ValidToDateTime and include it in the JOIN as AND condition
SELECT USDAMOUNT * dbo.EXCHANGERATEAMT
FROM dbo.Amount_monthly
LEFT JOIN dbo.EXCHANGERATE
ON dbo.Amount_monthly.Currencycode = dbo.EXCHANGERATE.fromcurrencycode
AND ValidToDateTime = (SELECT MAX(ValidToDateTime) --remove WHERE clause
FROM dbo.EXCHANGERATE)
AND dbo.EXCHANGERATE.EXCHANGERATETYPECODE = 'DAY';
I cleaned up your query a bit: as the other folks mentioned you needed to close the parentheses around the MAX(Date) sub-query, and if you reference a LEFT JOINed table in the WHERE clause, it behaves like an INNER JOIN, so I changed to in INNER. You also had "dbo" sprinkled in as a field prefix, but that (the namespace) only prefixes a database, not a field. I added the IS NOT NULL check just to avoid SQL giving the "null values were eliminated" SQL warning. I used the aliases "am" for the first table and "er" for the 2nd, which makes it more readable:
SELECT am.USDAMOUNT * er.EXCHANGERATEAMT
FROM dbo.Amount_monthly am
JOIN dbo.EXCHANGERATE er
ON am.Currencycode = er.fromcurrencycode
WHERE er.ValidToDateTime = (SELECT MAX(ValidToDateTime) FROM dbo.EXCHANGERATE WHERE ValidToDateTime IS NOT NULL)
AND er.EXCHANGERATETYPECODE = 'DAY'
If you're paranoid like I am, you might also want to make sure the exchange rate is not zero to avoid a divide-by-zero error.

Nesting COUNT in statement with JOIN

Really trying to figure out, why SQL query doesnt go through. I assume the structure is a bit wrong, but cant figure out where exactly. The references to tables are all correct.
SELECT tap_questionnaires.id,
tap_questionnaires.NAME,
tap_questionnaires.active,
tap_useranswers_ip.questionnaire_id,
Count(tap_useranswers_ip.ip)
FROM tap_questionnaires
LEFT JOIN tap_useranswers_ip
ON tap_questionnaires.id = tap_useranswers_ip.questionnaire_id
WHERE author_email = admin#admin.com
If you use count you need to use group by for the other columns in your select clause.
SELECT TAP_questionnaires.id, TAP_questionnaires.name, TAP_questionnaires.active, TAP_useranswers_ip.questionnaire_id, COUNT(TAP_useranswers_ip.ip) FROM TAP_questionnaires LEFT JOIN TAP_useranswers_ip on TAP_questionnaires.id=TAP_useranswers_ip.questionnaire_id WHERE author_email="admin#admin.com"
group by TAP_questionnaires.id, TAP_questionnaires.active
I think TAP_questionnaires.name it's not necessary because I suppose it depends on TAP_questionnaires.id. TAP_useranswers_ip.questionnaire_id is the same value as TAP_questionnaires.id
Hope that helps!
I think this version is clearer:
SELECT q.id, q.name, q.active, COUNT(a.ip)
FROM TAP_questionnaires q LEFT JOIN
TAP_useranswers_ip a
ON on q.id = a.questionnaire_id
WHERE author_email = 'admin#admin.com'
GROUP BY q.id, q.name, q.active;
Notes:
You need a GROUP BY.
You need single quotes around the string constant.
Table aliases make the query easier to write and to read.
There is no reason to include a.questionnaire_id. You already have q.id.

Issue in mysql query

I have 2 tables:
table 1:userdetails with fields:uid,mobile,name
table 2:accountdetails with fields:uid,savings,balance
Where uid is common and primary key in both tables.
Now Iam trying to get mobile,savings values from both tables where uid =1 how can we get.I tried below but didnt worked.
select mobile,savings
from userdetails,accountdetails
where userdetails.uid='1'AND
userdetails.uid = accountdetails.uid
Please can some one help
More modern version using the JOIN syntax:
SELECT
a.`mobile`,
b.`savings`
FROM `userdetails` a
JOIN `accountdetails` b
ON a.`uid` = b.`uid`
WHERE a.`uid` = 1
The query should probably look like this:
select ud.mobile, ad.savings
from userdetails ud left join
accountdetails ad
on ud.uid = ad.uid
where ud.uid = 1;
Notes:
Use proper, explicit JOIN syntax.
Use meaningful table aliases and qualify all columns names.
The left join keeps all rows, even if there is no match in the second table. That might be your problem.
I am assuming that uid is actually a number. Don't put numbers around numeric constants.
You could turn on your error display to show you exactly what line is causing the issue.
ini_set('display_errors', 1);
You can try this.
SELECT `ud`.`mobile`, `ad`.`savings`
FROM `userdetails` `ud`
INNER JOIN `accountdetails` `ad` ON `ud`.`uid` = `ad`.`uid`
WHERE `us`.`uid` = 1
Use join
select mobile,savings
from userdetails join accountdetails on userdetails.uid = accountdetails.uid
where userdetails.uid='1'

How to reformulate an sql query

i am not an expert in sql programming and i was wondering if you can help me reformulate the following query using INNER JOIN?
db_query("
select max(field_date_and_time_value2) as last_time
from field_data_field_date_and_time
where (field_date_and_time_value2 > '".$today."')
AND (".$node->uid." = (select uid from node where nid = " . $node->nid ."))");
Would something like this work?
SELECT
max(a.field_date_and_time_value2) as last_time
, b.uid
FROM field_data_field_date_and_time a
INNER JOIN
node b
ON
/* this piece might be wrong. what is the relationship between the first table and the second one? */
b.nid = '".$node->nid."'
where
a.field_date_and_time_value2 > '".$today."' AND
b.uid = $node->nid
Assuming your field_date_and_time table has a column called uid having the same meaning as that column in your node table, here's what you need to do.
SELECT MAX(A.field_date_and_time_value2) AS last_time
FROM field_date_and_time A
INNER JOIN node B ON A.uid = B.uid
WHERE B.nid = '".$node->nid."'
AND A.field_date_and_time_value2 > '".$today."'
Beware SQL injection attacks! Avoid substituting variables directly into SQL statements unless you're really sure those variables can't be corrupted by badguy users! Remember the story of the guy named
"johnny ; drop tables *;"