I have several tables that I am selecting data from. I am trying to get a list of information out of these tables only where the fields
"interested_parties.emp_id" and "solicitation_entrys.sol_ent_id" are satisfied.
This is my query that I have come up with so far:
SELECT * FROM users
JOIN interested_parties ON (users.emp_id = interested_parties.emp_id)
JOIN department_colors ON (department_colors.dept_id = users.emp_dept)
JOIN solicitation_entrys ON (solicitation_entrys.sol_ent_id = interested_parties.sol_ent_id)
WHERE IS NOT NULL (interested_parties.emp_id, solicitation_entrys.sol_ent_id)
The goal is to select everything from these tables, where these fields are not null.
I'm getting #1064 errors from MySQL and I can't seem to solve the issue. Do I have to perform the if at the beginning of the select? If so, how would you do that with joins?
Thank you for your help.
Use this instead
SELECT * FROM users
JOIN interested_parties ON (users.emp_id = interested_parties.emp_id)
JOIN department_colors ON (department_colors.dept_id = users.emp_dept)
JOIN solicitation_entrys ON (solicitation_entrys.sol_ent_id = interested_parties.sol_ent_id)
WHERE interested_parties.emp_id IS NOT NULL
AND solicitation_entrys.sol_ent_id IS NOT NULL
You have to compare each field for not being null.
As MySQL should telling you, the error is in your SQL syntax.
The WHERE clause should be:
WHERE (interested_parties.emp_id IS NOT NULL)
AND (solicitation_entrys.sol_ent_id IS NOT NULL)
Related
I have the following MySQL statement which does what I want:
SELECT scores.score, registrations.parade, AVG(scores.score) as result
FROM scores
JOIN registrations ON scores.registrationId=registrations.id
where registrations.parade=1
GROUP BY scores.registrationId
ORDER BY result DESC
basically, with sqlalchemy I think I would start with:
db.session.query(Scores, func.avg(Scores.score).label('result'))
This is because I do not need the information from registrations (it's linked to each other in the model). I only join the registrations in the MySQL statement because I need to filter on its parade.id
Below is what I have been trying so far but does not work:
scores = db.session.query(Scores,func.avg(Scores.score).label('result'))\
.filter(Registrations.parade == 1)\
.group_by(Scores.registrationId)\
.order_by(desc('result'))
You are missing the join
scores = db.session.query(Scores.score,func.avg(Scores.score).label('result'))\
.join(Registrations)\
.filter(Registrations.parade == 1)\
.group_by(Scores.registrationId)\
.order_by(desc('result'))
Another issue is, that you should have an aggretation function for registrations.parade or else include it in the group_by statement.
I am trying to select all data from databases Startuptier1 and connections where the company id = $select. Both databases have the column 'companyid' with the corresponding row.
None of the information regarding joins that I have found so far online is working. I have also tried unions. I assume I am missing something very obvious?
$sql = "SELECT * FROM startuptier1
JOIN connections ON startuptier1.companyid = connections.companyid
WHERE companyid= '$select';";
I expect to get the data from both databases from what I've read so far but all I'm getting is an SQL Error.
You are getting an error probably because, companyid is present in both tables and mysql cannot decide on which column to fire where condition:
Try the modified query:
$sql = "SELECT * FROM startuptier1
JOIN connections ON
startuptier1.companyid = connections.companyid
WHERE startuptier1.companyid in (".$select.")";
Note: Your database account should have appropriate permission. You should be able to perform operations on every database that you are accessing via joins
EXAMPLE 1:
SELECT * FROM DB1.table1 t1 JOIN DB2.table2 t2 ON t2.column2 = t1.column1;
EXAMPLE 2:
SELECT db1.artist.name, db2.painting.title
FROM db1.artist INNER JOIN db2.painting
ON db1.artist.a_id = db2.painting.a_id;
New to doing SQL stuff so please excuse me.
I want to create a SQL query that joins a column to table A from table B based on the following match logic:
B.Source = ‘SOURCE1’ and A.NameCode= B.Code
If the above return NULL then I’d like to match on:
B.Source <> ‘SOURCE1’ and A.UEN = B.UEN**
Any help on how to structure this?
I currently have a union all select query that can get the values based on the above conditions. Should I be using an If/or/case_when in the join process?
A few questions in which I thought could be helpful and that I've looked at are:
How to perform a LEFT JOIN in SQL Server between two SELECT statements?
Using IS NULL or IS NOT NULL on join conditions - Theory question
But I was not able to come up with anything :(
Thank you so much!
Try something like this:
SELECT *
FROM A
JOIN B ON (
(B.Source = 'Source1' AND A.NameCode = B.Code) OR
(B.Source <> 'Source1' AND A.UEN = B.[UEN**]) --Not sure what the ** is? Part of the field name?
)
I have written a join query in MySQL which works well and shows the result.
I am trying to write a MySQL query that shows 2 additional columns with some calculations
If isPercent=1 then
New Column1=price*currentPercent/100
New Column2=LineItemQuantity*price
I tried to write this query in PHP but since there are 100,000s records it is timing out.
Here is MySQL query and the results shown below
Select
wl.LineItems_LineItemID,
wl.LineItemQuantity,
pj.IsPercent,
pj.CurrentPercent,
pj.CurrentRate,
cb.Price
from
WorkOrderLineItems wl,
PayScaleLoaclJObCodes pj,
ClientBillingRates cb
where
wl.LineItems_LineItemID=pj.JobCodeID
AND wl.LineItems_LineItemID=cb.ClientBillingRates_ID
AND pj.PayScalesLocal_ID='33'
I would write the query this way:
SELECT
wl.LineItems_LineItemID,
wl.LineItemQuantity,
pj.IsPercent,
pj.CurrentPercent,
pj.CurrentRate,
cb.Price,
IF(pj.IsPercent=1, cb.Price*pj.CurrentPercent/100, NULL) AS `New Column 1`,
IF(pj.IsPercent=1, wl.LineItemQuantity*cb.Price, NULL) AS `New Column 2`
FROM
WorkOrderLineItems wl
JOIN PayScaleLoaclJObCodes pj ON wl.LineItems_LineItemID = pj.JobCodeID
JOIN ClientBillingRates cb ON wl.LineItems_LineItemID = cb.ClientBillingRates_ID
WHERE pj.PayScalesLocal_ID = '33'
As in the comments above, I encourage you to use JOIN syntax instead of relying on old-fashioned comma-style joins.
As for the query timing out, I would guess that you don't have the right indexes to support this query. If you want help with query optimization, you should run SHOW CREATE TABLE <tablename> for each table in your query, and post the output in your question.
I am currently running this SQL
SELECT jm_recipe.name, jm_recipe.slug
FROM jm_recipe
LEFT JOIN jm_category_recipe ON jm_category_recipe.recipe_id = jm_recipe.id
WHERE jm_category_recipe.category_id = $cat"
This returns the desired results except that I also need to return the name of the category that the recipe I am looking for is in, to do this I tried to add the field in to my SELECT statement and also add the table into the FROM clause,
SELECT jm_recipe.name, jm_recipe.slug, jm_category_name
FROM jm_recipe, jm_category
LEFT JOIN jm_category_recipe ON jm_category_recipe.recipe_id = jm_recipe.id
WHERE jm_category_recipe.category_id = $cat"
However this just returns no results, what am i doing wrong?
You need to join both tables:
SELECT jm_recipe.name, jm_recipe.slug, jm.category_name
FROM jm_recipe
INNER JOIN jm_category_recipe ON jm_category_recipe.recipe_id = jm_recipe.id
INNER JOIN jm_category ON jm_recipe.recipe_id = jm_category.recipe_id
WHERE jm_category_recipe.category_id = $cat
I've changed the joins to inner joins as well. You might want to make them both LEFT joins if you have NULLs and want them in the result.
Also, you're vulnerable to SQL Injection by simply copying over $cat.
Here's some PHP specific info for you (I'm assuming you're using PHP.)