I want to select all posts that match a list of tag Id's. I tried the following:
SELECT * FROM posts as t WHERE (SELECT tag_id FROM post_tags WHERE post_tags.post_id = t.id) = ALL (8, 1)
This gives the following error:
Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 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 '8, 1))'
I would be glad if someone knows how to solve this.
SELECT p.*
FROM posts p
JOIN post_tags pt
ON pt.post_id = p.id
WHERE tag_id IN(1,8)
GROUP
BY p.id
HAVING COUNT(*) = 2
(where '2' is equal to the number of items in IN()).
Related
I select some data from the mySQL database. I want select only the data where project_id is not NULL or empty:
$result = $db->query('SELECT *
FROM data
LEFT JOIN projects ON data.project=projects.project_id
GROUP BY data.id
WHERE projects.project_id IS NOT NULL
ORDER BY data.id DESC
')->fetchAll(PDO::FETCH_ASSOC);
but I get an error message:
Uncaught PDOException: SQLSTATE[42000]: Syntax error or access
violation: 1064 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 'WHERE projects.project_id IS NOT NULL ORDER BY d' at line 21
where clause comes before group by
I believe you are already having primary keys, you don't need to add the null check explicitly. If you need the matching dataset only, use inner join instead of left join.
There should be a proper syntax for query as group by will be before than where .
Query should be
SELECT * FROM data
LEFT JOIN projects ON data.project=projects.project_id
WHERE projects.project_id IS NOT NULL
GROUP BY data.id
ORDER BY data.id DESC
This should work, you are just not following the correct syntax with where and order by
$result = $db->query('SELECT *
FROM data
LEFT JOIN projects ON data.project=projects.project_id
WHERE projects.project_id IS NOT NULL
GROUP BY data.id
ORDER BY data.id DESC
')->fetchAll(PDO::FETCH_ASSOC);
Really stumped on this one. It should be a simple left join to a second table but i'm getting a syntax error. Trying to get the last due date on incomplete items.
Code:
SELECT *
FROM TBLTICKETHEADER h,
LEFT JOIN (SELECT HEADERID, MAX(DUEDATE)
FROM TBLTICKETITEM
WHERE YEAR(COMPLETEDDATE) = 9999
GROUP BY HEADERID) ld ON ld.HEADERID = h.HEADERID
ERROR:
1064 - 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 'LEFT JOIN (SELECT HEADERID, MAX(DUEDATE)
FROM TBLTICKETIT' at line 3
You have a comma after your h on the from clause. Remove it and your query should run.
SELECT *
FROM TBLTICKETHEADER h
LEFT JOIN (SELECT HEADERID, MAX(DUEDATE)
FROM TBLTICKETITEM
WHERE YEAR(COMPLETEDDATE) = 9999
GROUP BY HEADERID) AS ld ON ld.HEADERID = h.HEADERID
I got this query:
SELECT companies_comments.id as id,
companies_comments.post as post,
companies_comments.comment as comment,
companies_comments.date as date,
companies.`id` AS company,
companies.`name` as name,
companies.`username` as username,
companies.`photo` as photo,
companies.`status` AS company_status
LEFT JOIN companies ON companies.id = companies_comments.company
WHERE company_status NOT IN (3,4)
AND companies_comments.post =1
The error: Error Code: 1064. 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 'LEFT JOIN companies ON companies.id = companies_comments.company WHERE compa' at line 10
Error 1064 unexpectedly. Just tried with no ` , same results. No missing column. What can be happening?
You're missing the tablename and also a FROM section in the query
SELECT companies_comments.id as id,
companies_comments.post as post,
companies_comments.comment as comment,
companies_comments.date as date,
companies.`id` AS company,
companies.`name` as name,
companies.`username` as username,
companies.`photo` as photo,
companies.`status` AS company_status
FROM companies, companies_comments
LEFT JOIN companies ON companies.id = companies_comments.company
WHERE companies.company_status NOT IN (3,4)
AND companies_comments.post =1
You haven't added FROM what table.You have missed the from part.
Example:
SELECT *
FROM table1
LEFT JOIN table2
ON table1.column_name=table2.column_name
Please have a look at the below query.
SELECT sub_words.idwords, words_inc.idArticle
(
SELECT sub0.idwords, SUBSTRING_INDEX(GROUP_CONCAT(sub1.idwords), ',', 10) AS excl_words, COUNT(sub1.idwords) AS older_words_cnt
FROM words_learned sub0
LEFT OUTER JOIN words_learned sub1
ON sub0.userId = sub1.userId
AND sub0.order < sub1.order
WHERE sub0.userId = 1
GROUP BY sub0.idwords
) sub_words
INNER JOIN words words_inc
ON sub_words.idwords = words_inc.idwords
LEFT OUTER JOIN words words_exc
ON words_inc.idArticle = words_exc.idArticle
AND FIND_IN_SET(words_exc.idwords, sub_words.excl_words)
WHERE words_exc.idwords IS NULL
ORDER BY older_words_cnt
LIMIT 100
This gives the error
#1064 - 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 'SELECT sub0.idwords, SUBSTRING_INDEX(GROUP_CONCAT(sub1.idwords), ',', 10) AS exc' at line 3
I checked the sub query individually and there was no error in the sub query! what is going on here?
You missed the from keyword
SELECT sub_words.idwords, words_inc.idArticle
FROM
( ...
Please can anyone tell me what's wrong with this MySQL query
SELECT count(items.id) AS numrows
FROM items, subcategories
WHERE items.`base_school` = 'UNN'
AND items.subcategory = subcategories.id
AND subcategories.parent_category = 3
JOIN `item_images`
ON items.id=`item_images`.`item_id`
It keeps throwing up this error:
#1064 - 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 'JOIN item_images ON items.id=item_images.item_id' at line
1
You have the WHERE clause in the wrong location, try this:
SELECT count(items.id) AS numrows
FROM items
INNER JOIN subcategories
ON items.subcategory = subcategories.id
INNER JOIN item_images
ON items.id=item_images.item_id
WHERE items.base_school = 'UNN'
AND subcategories.parent_category = 3