I was hoping someone could help me figure out what I am doing wrong with a SQL query I am trying to run from within Sequel Pro. The error message says
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.id
INNER JOIN directory_person ON directory_twitter.id = directory_person.id
WH' at line 1
And the query that I have written is
SELECT directory_twitter.id, directory_twitter.name, directory_twitter.screen_name,
directory_twitter.followers, directory_group.id, directory_group.title
FROM directory_twitter, directory_group
INNER JOIN directory_person_groups
ON directory_person_groups.person_id = directory_twitter.id,
directory_person_groups.group_id = directory_group.id
INNER JOIN directory_person
ON directory_twitter.id = directory_person.id
WHERE directory_person.appears_in_directory = "1"
I am trying to get the query to return the name of the users (directory_twitter.name), their screen name (directory_twitter.screen_name), their total number of followers (directory_twitter.followers), and the name of the group they are in (directory_group.title). Unfortunately the way our tables are set up, the only way I can think to join the group to the user is by INNER JOIN-ing a third table (directory_person_groups) where the group ID and the user ID are both present.
Finally, the only users that I want to be returned by this query are those who are in our directory (WHERE directory_person.appears_in_directory = "1"). In the table directory_person, directory_person.id = directory_twitter.id.
I have been trying to figure out what my error is for hours and I've made no progress. Is everything correct except a syntax error that I am unfamiliar with? Any and help is greatly appreciated. Thank you!
EDIT: All of the columns of the tables that I'm querying are below.
directory_twitter: id, person_id (which is the same value as id), screen_name, name, followers, user_id (I'm not sure where else this selector is used in the database, it is a different value from id and person_id).
directory_group: id (different from directory_twitter.id), slug(a slug of the title), title, screen_name (the Twitter handle of the group, e.g. CNN for #cnn)
directory_person_groups: id (I'm not sure where else if anywhere this value appears in the database, it is different from both directory_twitter.id and directory_group.id), person_id, group_id
directory_person: id (the same as directory_twitter.id which is the same as directory_twitter.person_id), title (this value is different from directory_group.title), first_name, last_name, appears_in_directory
Try this query but you need to add some indexes or it will take a very long time to run. I would suggest you make the columns in WHERE clause as indexes before I run it if I were you.
SELECT DT.name, DT.screen_name, DT.followers, DG.title
FROM directory_twitter AS DT, directory_group AS DG, directory_person_groups AS DPG
WHERE DPG.person_id=DT.person_id AND DPG.group_id=DG.id AND DT.person_id IN
( SELECT DP.id
FROM directory_person AS DP
WHERE appears_in_directory='1'
) ;
Try this
SELECT DIRECTORY_TWITTER.ID,
DIRECTORY_TWITTER.NAME,
DIRECTORY_TWITTER.SCREEN_NAME,
DIRECTORY_TWITTER.FOLLOWERS,
DIRECTORY_GROUP.ID,
DIRECTORY_GROUP.TITLE
FROM DIRECTORY_TWITTER,
DIRECTORY_PERSON_GROUPS,
DIRECTORY_PERSON,
DIRECTORY_GROUP
INNER JOIN DIRECTORY_PERSON_GROUPS
ON DIRECTORY_PERSON_GROUPS.PERSON_ID = DIRECTORY_TWITTER.ID
AND DIRECTORY_PERSON_GROUPS.GROUP_ID = DIRECTORY_GROUP.ID
INNER JOIN DIRECTORY_PERSON
ON DIRECTORY_TWITTER.ID = DIRECTORY_PERSON.ID
WHERE DIRECTORY_PERSON.APPEARS_IN_DIRECTORY = "1"
I changed the comma to AND
Related
I'm trying to fetch some user data from my two tables. Any idea what's wrong? Any help is appreciated.
Error message:
Unknown column 'a3p_user_fields.user_id' i tabel field list
My tables:
**a3p_user_fields**
user_id,
field_title,
field_job_title,
field_education,
field_experience,
field_hourly_wage,
field_support,
field_fee,
field_description,
**a3p_usermeta**
umeta_id,
user_id,
meta_key,
meta_value
My query:
SELECT `a3p_user_fields.user_id`, `a3p_user_fields.field_job_title`,
`a3p_user_fields.field_education`, `a3p_user_fields.field_experience`,
`a3p_user_fields.field_hourly_wage`, `a3p_usermeta.meta_key` FROM
`a3p_user_fields` INNER JOIN `a3p_usermeta`
ON `a3p_user_fields.user_id` = 'a3p_usermeta.user_id`
WHERE `a3p_user_fields.field_title` = 'Gartner'
AND `a3p_usermeta.meta_key` = 'candidate_flexjob' AND `a3p_usermeta.metavalue` = 'Ja'
ORDER BY `a3p_user_fields.user_id` ASC
Problem is in below lines I believe which is a quoting issue
ON `a3p_user_fields.user_id` = 'a3p_usermeta.user_id`
You meant it to be
ON `a3p_user_fields`.`user_id` = `a3p_usermeta`.`user_id`
As well your way of selecting the columns are not correct like
SELECT `a3p_user_fields.user_id`
You should quote the table and column names separately
SELECT `a3p_user_fields`.`user_id`
I have been through a few other posts relating to my error, but none of the solutions seem to work. I'm fairly new to SQL so sorry if its something really simple. I have two tables
Movie Inventory - which has columns movie_title, onhand_qty, and replacement_price
NotFlix - which has subscriber_name, queue_nbr, and movie_title
I am trying to join the two tables to output the total replacement price cost per customer, but when I do it gives me the error titled above. Here is my code, thanks in advance for any help!
SELECT subscriber_name, SUM (replacement_price) as replacement
FROM
(SELECT NotFlix.subscriber_name, NotFlix.movie_title, NotFlix.queue_nbr, MovieInventory.replacement_price
FROM NotFlix
INNER JOIN MovieInventory
ON NotFlix.movie_title = MovieInventory.movie_title
)
GROUP BY subscriber_name;
You are missing an alias:
SELECT AliasNameHere.subscriber_name, SUM (AliasNameHere.replacement_price) as replacement
FROM
(SELECT NotFlix.subscriber_name as subscriber_name, NotFlix.movie_title, NotFlix.queue_nbr, MovieInventory.replacement_price as replacement_price
FROM NotFlix
INNER JOIN MovieInventory
ON NotFlix.movie_title = MovieInventory.movie_title
) AliasNameHere
GROUP BY subscriber_name;
I Just don't get why are you doing a temporary table in FROM clause, you could just do a basic INNER JOIN here and potientialy avoid problem with alias name :
SELECT NotFlix.subscriber_name, SUM (MovieInventory.replacement_price) as replacement
FROM NotFlix
INNER JOIN MovieInventory
ON NotFlix.movie_title = MovieInventory.movie_title
GROUP BY subscriber_name;
This is a very specific question to the kind of code i have written for postgresql and i am migrating to mysql for project requirements.
The code written so far in mysql is as follows :
(select substring(dt,1,9) as dt,concat(vish,visl,visn) as vis,ip
from assignment_walmart.b
where service='ss' and ua not like '%ktxn%'
and ua not like '%khte%'
and ua not like '%keynote%'
group by 1,2,3
) as A1
left join // This is where it shows the error.
(select ip,flag from
assignment_walmart.b1
group by 1,2
) as A2
on A1.ip=A2.ip
where A2.flag is NULL
group by 1,2;
The error is popping up near the naming of the two selected tables as "A1" and "A2", so i'm assuming it's not allowed in mysql.
Can you please help me with an alternate syntax for the above code as I have to use the two tables in this manner only to join in to get required results.
How exactly do i use alias or join 2 tables in such a manner which was clearly working in postgresql?
Any help would be appreciated.
You have a subquery joined to another query. This shouldn't work in either database. You need to wrap these in a select or something like that:
select A2.dt, A2.vis, count(*)
from (select substring(dt,1,9) as dt, concat(vish,visl,visn) as vis,ip
from assignment_walmart.b
where service='ss' and ua not like '%ktxn%'
and ua not like '%khte%'
and ua not like '%keynote%'
group by substring(dt,1,9), concat(vish,visl,visn), ip
) as A1 left join // This is where it shows the error.
(select ip,flag from
assignment_walmart.b1
group by ip, flag
) as A2
on A1.ip=A2.ip
where A2.flag is NULL
group by A2.dt, A2.vis;
I am making a guess on what you want for the outer query and what the aggregation fields are. It is a good idea to be explicit about which fields are being aggregated.
It looks like you are missing the SELECT ... FROM on the outer query.
It looks you mean for your query to be of the form:
SELECT ...
FROM ( inline view query ) A1
LEFT
JOIN ( inline view query ) A2
ON A1.col = A2.col ...
WHERE ...
GROUP BY ...
I'm a new code having trouble getting a subquery to work with the main query data. Essentially I want to count how many PCs are HP PCs from the results. When I run it I get the error: The multi-part identifier "dbo.softwareapplications.softwareid" could not be bound.
select distinct appname, version, linkid,
(select count(make) as totalhp from dbo.workstations where make = 'Hewlett-Packard' and linkdid = t1.linkid)as totalhp
from dbo.softwareapplications as t1
join dbo.assignments on dbo.softwareapplications.softwareid = dbo.IQCSassignments.softwareid
join dbo.workstations on dbo.assignments.wsid = dbo.workstations.wsid
group by assetdescription, version, linkid, totalhp
Any help would be appreciated.
Since you've aliased the "dbo.softwareapplications" table as t1, that's how you have to refer to it in subsequent clauses:
join dbo.assignments on t1.softwareid = dbo.IQCSassignments.softwareid
(Or alternatively, remove the "as t1" alias and the other reference to it.)
Use the alias you created : t1.softwareid instead of dbo.softwareapplications.softwareid
MySQL Server Version: Server version: 4.1.14
MySQL client version: 3.23.49
Tables under discussion: ads_list and ads_cate.
Table Relationship: ads_cate has many ads_list.
Keyed by: ads_cate.id = ads_list.Category.
I am not sure what is going on here, but I am trying to use COUNT() in a simple agreggate query, and I get blank output.
Here is a simple example, this returns expected results:
$queryCats = "SELECT id, cateName FROM ads_cate ORDER BY cateName";
But if I modify it to add the COUNT() and the other query data I get no array return w/ print_r() (no results)?
$queryCats = "SELECT ads_cate.cateName, ads_list.COUNT(ads_cate.id),
FROM ads_cate INNER JOIN ads_list
ON ads_cate.id = ads_list.category
GROUP BY cateName ORDER BY cateName";
Ultimately, I am trying to get a count of ad_list items in each category.
Is there a MySQL version conflict on what I am trying to do here?
NOTE: I spent some time breaking this down, item by item and the COUNT() seems to cause the array() to disappear. And the the JOIN seemed to do the same thing... It does not help I am developing this on a Yahoo server with no access to the php or mysql error settings.
I think your COUNT syntax is wrong. It should be:
COUNT(ads_cate.id)
or
COUNT(ads_list.id)
depending on what you are counting.
Count is an aggregate. means ever return result set at least one
here you be try count ads_list.id not null but that wrong. how say Myke Count(ads_cate.id) or Count(ads_list.id) is better approach
you have inner join ads_cate.id = ads_list.category so Count(ads_cate.id) or COUNT(ads_list.id) is not necessary just count(*)
now if you dont want null add having
only match
SELECT ads_cate.cateName, COUNT(*),
FROM ads_cate INNER JOIN ads_list
ON ads_cate.id = ads_list.category
GROUP BY cateName
having not count(*) is null
ORDER BY cateName
all
SELECT ads_cate.cateName, IFNULL(COUNT(*),0),
FROM ads_cate LEFT JOIN ads_list
ON ads_cate.id = ads_list.category
GROUP BY cateName
ORDER BY cateName
Did you try:
$queryCats = "SELECT ads_cate.cateName, COUNT(ads_cate.id)
FROM ads_cate
JOIN ads_list ON ads_cate.id = ads_list.category
GROUP BY ads_cate.cateName";
I am guessing that you need the category to be in the list, in that case the query here should work. Try it without the ORDER BY first.
You were probably getting errors. Check your server logs.
Also, see what happens when you try this:
SELECT COUNT(*), category
FROM ads_list
GROUP BY category
Your array is empty or disappear because your query has errors:
there should be no comma before the FROM
the "ads_list." prefix before COUNT is incorrect
Please try running that query directly in MySQL and you'll see the errors. Or try echoing the output using mysql_error().
Now, some other points related to your query:
there is no need to do ORDER BY because GROUP BY by default sorts on the grouped column
you are doing a count on the wrong column that will always give you 1
Perhaps you are trying to retrieve the count of ads_list per ads_cate? This might be your query then:
SELECT `ads_cate`.`cateName`, COUNT(`ads_list`.`category`) `cnt_ads_list`
FROM `ads_cate`
INNER JOIN `ads_list` ON `ads_cate`.`id` = `ads_list`.`category`
GROUP BY `cateName`;
Hope it helps?