I'm trying to run this code but i'm getting errors.
Any ideas why?
SELECT notes.project_id, notes.title, notes.description
FROM notes, project_members
WHERE notes.title LIKE '%second%' OR notes.description LIKE '%second%'
WHERE notes.project_id = project_members.project_id AND project_members.user_id = '7'
Since you only can have one WHERE clause you could use a inner join like this:
SELECT notes.project_id, notes.title, notes.description
FROM project_members pm
INNER JOIN notes ON notes.project_id = pm.project_id AND pm.user_id = 7
WHERE notes.title LIKE '%second%' OR notes.description LIKE '%second%'
You have two WHERE clauses, but single SELECT only allows one. But, this would easily be fixed if you used proper, explicit JOIN syntax:
SELECT n.project_id, n.title, n.description
FROM notes n JOIN
project_members p
ON n.project_id = p.project_id
WHERE (n.title LIKE '%second%' OR n.description LIKE '%second%') AND
p.user_id = '7';
Note:
Always use proper, explicit JOIN syntax. Never use commas in the FROM clause.
Table aliases make the query easier to write and to read.
I'm pretty sure your WHERE conditions are missing parentheses, for your intended logic.
If user_id is a number of any sort, then don't use single quotes around "7". This can confuse both the compiler and people.
There should be single WHERE clause.
SELECT notes.project_id, notes.title, notes.description
FROM notes, project_members
WHERE
(notes.title LIKE '%second%' OR notes.description LIKE '%second%')
AND notes.project_id = project_members.project_id AND project_members.user_id = '7';
Query would look something above to AND between your LIKE conditions.
You may also consider using JOIN, Like this.
SELECT notes.project_id, notes.title, notes.description
FROM notes
JOIN project_members
ON notes.project_id=project_members.project_id
WHERE
(notes.title LIKE '%second%' OR notes.description LIKE '%second%')
AND project_members.user_id = '7';
Related
I'm making the below query:
SELECT
`user_organisation`.*,
`users`.`full_name`,
`users`.`email`,
`users`.`avatar`
FROM
`user_organisation`
INNER JOIN `users` ON `users`.`id` = `user_organisation`.`user_id`
WHERE
`users`.`full_name` LIKE '%test%' OR `users`.`email` LIKE '%test%' AND `user_organisation`.`organisation_id` = 111
And I get the results containing user_organisation.organisation_id equals 111 and 222. I want to search records with users.full_name or users.email containing value test. This SQL successfully searches users with full name or email containing string test but not only user_organisation.organisation_id=111.
What the reason for that unexpected behaviour? I sure I'm missing something but I can't see...
You need parentheses in your WHERE clause around the two ORed terms:
SELECT uo.*, u.full_name, u.email, u.avatar
FROM user_organisation uo
INNER JOIN users u ON u.id = uo.user_id
WHERE (u.full_name LIKE '%test%' OR u.email LIKE '%test%') AND
uo.organisation_id = 111;
Note also that in my answer I am using table aliases (which are abbreviations for full table names). Also, I removed the unnecessary backticks.
I need something similar to this ... but its not working obviously, not sure if I need to use any JOIN's or what. Much help is appreciated! Thanks.
vicidial_closer_log.user = vicidial_users.user = vicidial_campaign_agents.user
SELECT
vicidial_closer_log.call_date,
Count(vicidial_closer_log.`status`) AS call_count,
vicidial_closer_log.`user`,
vicidial_closer_log.user_group,
vicidial_users.`user`,
vicidial_users.full_name,
vicidial_campaign_agents.`user`,
vicidial_campaign_agents.campaign_rank
FROM
vicidial_closer_log ,
vicidial_users ,
vicidial_campaign_agents
WHERE
date_format(vicidial_closer_log.call_date, '%Y-%m-%d') BETWEEN '2014-04-02' AND '2014-04-02' AND
vicidial_closer_log.`user` NOT LIKE 'VDCL' AND
vicidial_closer_log.`status` NOT LIKE 'CS' AND
vicidial_closer_log.`status` NOT LIKE 'NANQUE' AND
vicidial_closer_log.`status` NOT LIKE 'TIMEOT' AND
vicidial_closer_log.user_group IN ('RAD_US', 'RAD_JAM') AND
vicidial_closer_log.`user` = vicidial_users.`user`
GROUP BY
vicidial_closer_log.`user`
ORDER BY
vicidial_closer_log.`user` ASC
The way you were doing putting the [vicidial_closer_log.user = vicidial_users.user] on WHERE clause isn't completly wrong, but you forgot to link the vicidial_campaign_agents table. If you add a
AND vicidial_users.user = vicidial_campaign_agents.user
at the end of WHERE clause should work to, but try getting used to the JOIN clause. It's the right way of doing it and makes your query look prettier.
The JOIN clause is very simple, works like this: table_a JOIN table_b ON table_a.key = table_b.key. The ON clause specifies what column must match between table_a and table_b and then you JOIN the result of this with table_c doing the same thing.
LIKE THIS:
SELECT
l.call_date,
Count(l.status) AS call_count,
l.user,
l.user_group,
u.full_name,
a.campaign_rank
FROM
vicidial_closer_log l
JOIN vicidial_users u ON l.user = u.user
JOIN vicidial_campaign_agents a ON u.user = a.user
WHERE
date_format(l.call_date, '%Y-%m-%d') BETWEEN '2014-04-02' AND '2014-04-02' AND
l.user <> 'VDCL' AND
l.status NOT IN ('CS', 'NANQUE', 'TIMEOT') AND
l.user_group IN ('RAD_US', 'RAD_JAM')
GROUP BY
l.user
ORDER BY
l.user ASC
I've also added alias to you tables, so your query gets cleaner.
And also, you don't neet simple quotes to specify table columns like table_a.'column', just type table_a.column.
And also, the part you typed
l.status NOT LIKE 'CS' AND
l.status NOT LIKE 'NANQUE' AND
l.status NOT LIKE 'TIMEOT'
you could have typed
l.status NOT IN ('CS', 'NANQUE', 'TIMEOT')
And also, you're selecting l.user, u.user and a.user. The way you described your query, these 3 columns will aways be identical on each line, so it's a waste of network traffic when downloading the resultset from the DB server.
I hope it works.
I have a sql statement I've created, and I need to transform it to use explict join operators so that all compare against constant clauses, and only compare against constant clauses, appear in the where clause for the query.
I am not sure how to make this change though, can anyone show me how I would do this? Here is what I have:
select S.sname
from P, J, S, SPJ
where P.pname = 'Bolt'
and J.city = 'London'
and P.p# = SPJ.p#
and J.j# = SPJ.j#
and S.s# = SPJ.s#;
If I understand you, you are looking to convert from sql89 syntax to an inner join.
It would look like this:
select
S.sname
from
P
inner join SPJ on `P.p#` = `SPJ.p#` and P.pname = 'Bolt'
inner join J on `SPJ.j#` = `J.j#` and J.city = 'London'
inner join S on `SPJ.s#` = `S.s#`
I have added the pname and city restrictions to the join syntax because that appears to be what you asked for. These can be left in the where clause as well however.
Also note that extended or special characters in column names in mysql (like p#) must be enclosed in backticks.
You want something like this:
SELECT S.sname
FROM P INNER JOIN SPJ ON P.p#=SPJ.p#
INNER JOIN J ON J.j# = SPJ.j#
INNER JOIN S ON S.s# = SPJ.s#
WHERE P.pname = 'Bolt'
AND J.city = 'London';
The conditions that are used to combine tables are placed in the JOIN clauses, and the other conditions are left in the WHERE clause.
I'm using an opensource database, so it's setup is a bit over my head.
Its basically like this.
A persons normal information is in the table 'person_per'
There is custom information in the table 'person_custom'
both use 'per_ID' to organize.
select per_ID from person_custom where c3 like '2';
gives my the IDs of people who fit my search, I want to "join" (I think) their name, phone, ect from the 'person_per' table using the ID as the "key"(terms I read that seem to fit).
How can I do that in a single query?
select per.*
from person_per per
inner join person_custom cus on cus.per_id = per.per_id
where cus.c3 = 2
You can retrieve all the columns from both tables with a single query:
SELECT p.name
, p.phone
, p.ect
, c.custom_col
FROM person_per p
JOIN person_custom c
ON c.per_ID = p.per_ID
WHERE c.c3 LIKE '2'
Use a JOIN operator between the table names, and include the "matching" criteria (predicate) in the ON clause.
I am having following database schema, I want to fetch name of all categories with no of quotes related to that category . The query that i wrote giving me one row only can u please tell me the resource efficient query.
SELECT SC.Name, Count(*) AS Quotes
FROM status_categories AS SC
INNER JOIN status_quotes AS SQ ON SC._id = SQ._category_id
GROUP BY SC.Name
SELECT status_categories.NAME, COUNT(status_quotes.category_id)
FROM status_categories JOIN status_quotes ON status_categories._id = status_quotes.category_id
GROUP BY status_categories._id;
Try the following:
SELECT `c`.`name`, COUNT(*) AS `Number of quotes`
FROM `status_categories` AS `c`
INNER JOIN `status_quotes` AS `q`
ON `q`.`category_id` = `c`.`_id`
GROUP BY `c`.`_id`;
EDIT
Feel free to leave out the ` character. But that is the safe way of doing it, even though it looks a bit nasty.