Query a table based on values from within that table [duplicate] - mysql

This question already has answers here:
Using Substring_Index with data from a second table
(3 answers)
Closed 2 years ago.
We have a WordPress site that contains a Q&A plugin, that needs to be moved to a different hosting. The Q&A posts only make up for a fraction of the total Posts table, so I want to filter them out using a SQL query.
I want to select everything in the DB, through PhpMyAdmin, for the rows that match one of the following criteria:
post_type = "answer"
post_type = "question"
post_type contains revision, preceded by the ID of either one of the previous criteria. For example: 21-revision-v1 or 10903-revision-v1 Where I want to select those posts of which the first numerical part matches the ID of posts selected in the previous 2 requirements.
I am a complete novice to SQL so I started with some googling, and found the concept of Temporary Tables. Which lead me to create this bit of code:
SELECT * INTO #QA
FROM `wp_posts` WHERE
`post_type` = "answer" OR
`post_type` = "question"
However I get the following error:
#1064 - Er is iets fout in de gebruikte syntax bij 'FROM wp_posts WHERE
post_type = "answer" OR
post_type = "question" LIMI' in regel 2
Which translates to "There is somthing wrong with the syntax near"....
Is what I am attempting even feasible?

MySQL does not support the select ... into ... syntax to write to a table, as pinpointed in the documentation.
Instead you can use insert ... select:
insert into `#qa`
select *
from wp_posts
where post_type in ('question', 'answer')

The syntax that you are using is most commonly associated with SQL Server. MySQL uses the (more common) create table as syntax. And, it allows specifically for temporary tables in the syntax.
So, the equivalent in MySQL is CREATE TABLE AS:
CREATE TEMPORARY TABLE QA AS
SELECT p.*
FROM wp_posts p
WHERE post_type IN ('answer', 'question');
Note that a temporary tables is a very specific type of table that exists only in the current "session" -- say, your current connection to the database. It is not visible to other users and it will disappear when you reconnect to the database.

The SELECT * INTO... syntax is used to create a new table.
If this is what you want then the syntax for MySql is:
CREATE TABLE tablename AS
SELECT * FROM `wp_posts`
WHERE `post_type` = 'answer' OR `post_type` = 'question'

Related

Incorrect SQL Syntax when using JOIN [duplicate]

This question already has an answer here:
MySQL Inner Join Query Syntax error
(1 answer)
Closed 4 years ago.
In my DB I have 2 Tables. One for user Login information and one for general information.
Im trying to write a query that will select the column "firstname" from rows where the FK "users_id" is the same as the logged in users ID.
Before doing anything in PHP Im running the query in my database, so the logged in users ID, which would normally be a variable, is replaced with the id of my testuser.
This is my query:
SELECT b6vjp_user_info.firstname
FROM b6vjp_user_info
WHERE b6vjp_user_info.users_id LIKE 243
INNER JOIN b6vjp_users ON b6vjp_user_info.users_id=b6vjp_users.id;
And here is my (censored for security reasons) Login Table named "b6vjp_users":
And here is my other table named "b6vjp_user_info":
The error is:
#1064 - Mistake in SQL-Syntax. 'INNER JOIN b6vjp_users ON b6vjp_user_info.users_id=b6vjp_users.id LIMIT 0, 25' on row 4
Now fyi I translated that, because my work environment is in german. But im sure you know what a Syntax-Error is.
Anyways I checked the JOIN Part of my query over and over again and looked up the JOIN tutorial on W3Schools. But there is no apparent mistake.
Does anybody see what I somehow fail to?
Put the WHERE clause after the (last) ON clause.
I strongly recommend using table aliases. You also need to fix the order of your SQL clauses. So:
SELECT ui.firstname
FROM b6vjp_user_info ui JOIN
b6vjp_users u
ON ui.users_id = u.id
WHERE ui.users_id = '243';
Or, more simply without the JOIN:
SELECT ui.firstname
FROM b6vjp_user_info ui
WHERE ui.users_id = '243';
Notes:
The operand to LIKE should be a string. So, make it a string! Implicit type conversion causes all sorts of problems.
If you are not using wildcards, I think = is more informative. If users_id is really a number, then just use = 243 rather than LIKE.
WHERE goes after the FROM clause. JOIN is an operator in the FROM clause.
The JOIN is not necessary. Unless you are fetching columns from the users table (or need it for filtering which is highly doubtful), don't bother with it.
you have to put where after on clause
SELECT b6vjp_user_info.firstname
FROM b6vjp_user_info
INNER JOIN
b6vjp_users ON
b6vjp_user_info.users_id=b6vjp_users.id
WHERE b6vjp_user_info.users_id =243 // i think it int field so no need to use like operator

MySQL Error 1054 Unknown Column 'persons.PersonID' in on clause [duplicate]

This question already has answers here:
How can I do a FULL OUTER JOIN in MySQL?
(15 answers)
Closed 6 years ago.
Here i am trying to run one query in MySQL editor and getting one problem
Here is my query
select *
FROM my_db.persons FULL JOIN
my_db.employee
ON persons.PersonID=employee.PersonID;
Any help would be appreciated
MySQL doesn't support FULL JOIN, so perhaps that is the problem. In any case, I prefer shorter table aliases:
select *
FROM my_db.persons p LEFT JOIN
my_db.employee e
ON p.PersonID = e.PersonID;
This, of course, assumes that the PersonID column exists in both tables.
Oh, I see why you got the error. Perhaps this will explain:
select *
FROM my_db.persons full JOIN
my_db.employee e
ON full.PersonID = e.PersonID;
That is, because MySQL doesn't support FULL JOIN, the full is treated as a table alias.
Check if PersonID column exists on Persons table. Make sure the spellings are exactly the same as in the table structure. Also check the case. Some IDE are case sensitive.

Updating term_relationships with new categories from CSV

I am trying to update term_relationships with new categories established from a CSV, and am not too familiar with SQL.
I have already uploaded the CSV to the database and created a table called ut_csv, this table has several columns but the important ones are the ID and the category. The ID is the specific posts ID and the category already has the term_taxonomy_id.
I ran two scripts, the first being:
INSERT INTO wp_term_relationships
(object_id,term_taxonomy_id,term_order)
SELECT ID,category,0 FROM ut_csv
where post_type = 'post'
this failed with the error of #1054 - Unkown column post_type in where clause
So I ran the script, but dropped the where post_type = 'post, and this ran a success but when I checked the wp_term_taxonomy the 5 categories had no count.
Again I am unfamiliar with SQL so I may be doing something wrong or completely stupid. Any advice / help would be great.
Thanks

How do I do a DELETE in MySQL with NOT IN? [duplicate]

This question already has answers here:
MySQL Error 1093 - Can't specify target table for update in FROM clause
(16 answers)
Closed 7 years ago.
I'm getting this error when running the code:
Error Code: 1093. You can't specify target table 'details' for update
in FROM clause
DELETE FROM details WHERE detail NOT IN
(
SELECT detail
FROM user_details
JOIN data ON user_details.data_iddata = data.iddata
JOIN details ON details.iddetails = data.details_iddetails
)
What am I doing wrong here?
I don't have a system handy, but I think the problem is that you can reference the same table in a subquery as the main one you are deleting from. I.e. it doesn't like JOIN details ON details.iddetails = data.details_iddetails in the subquery because that's in the main select clause.
One workaround is to create a temp table, insert the records you are interested in into it, and then find your set from a join off of that.
You're trying to do a DELETE from a table while you're doing a SELECT from it.
In order to get this to work, make a list from your SELECT query and put its results in the parentheses.
If you aren't doing this in an external script, you should define a view that will give you the desired SELECT output.

MySQL query help moving data between tables

I've imported by phpbb3 forum in bbpress using the built-in importer. All of the anonymous users from bbpress who didn't have accounts, but were allowed to post are disconnected from there posts and everything is showing up as anonymous in bbpress. I grabbed all the post_usernames from phpbb_posts and created users with this query:
INSERT INTO wp_users (user_login)
SELECT DISTINCT post_username
FROM phpbb_posts
Now I'm trying to do a query between the 3 different tables. Something along these lines:
SELECT ID FROM wp_users
INSERT INTO wp_posts(post_author)
WHERE wp_posts(post_date) = phpbb_posts(post_time)
AND phpbb_posts(post_username) = wp_users(user_login)
Obviously this isn't right... probably syntax errors, but I also need to add some way of telling MySQL that the user_login has to be attached to the ID from the first line. Hopefully this makes sense. Thanks in advance for any help!
Updated queries:
SELECT ID FROM wp_users
SELECT post_time FROM phpbb_posts = post_date
SELECT post_username FROM phpbb_posts = user_login
hopefully this syntax makes more sense. These did work and they select the right information. The problem is I don't know how to write the WHERE statement properly and like you said baskint, I think I need to make the last statement a sub-query somehow. Thanks again!
I am still not sure what are the PK's (Primary Key) and FK's (Foreign Key) relationships of each table. However, assuming that wp_users is the primary table and phpbb_posts.post_username is the FK of wp_users.user_login...:
SELECT `wp_users`.`ID`
FROM `wp_users` INNER JOIN
(SELECT `phpbb_posts`.`post_username` FROM `phpbb_posts`, `wp_posts` WHERE `phpbb_posts`.`post_time` = `wp_posts`.`post_date` ) AS `posts`
ON `wp_users`.`user_login` = `posts`.`post_username`;
EDIT (Dec-05-2012):
After chatting and going through specific, #sbroways had to change data-types on some fields and a few other modifications. In turn, the final query turned out to be:
SELECT wp_users.*, ws_posts.*
FROM wp_users INNER JOIN ws_posts
ON wp_users.user_login = ws_posts.user_login
you're right. your syntax is confusing and not correct. trying to understand what you are trying to accomplish. in second query, why are you selecting and inserting at the same time? perhaps i am missing something, but can you state what you are trying to pull out from which tables and how you would like to see the results in plain English?
Also you can think in terms of sub-queries (SELECT * FROM b WHERE id IS IN (SELECT Id from a). You can cascade this a few times and perhaps get to your answer.