Delete users with no posts in WordPress using SQL query - mysql

I need to write a SQL script to delete users with no posts associated with them in a WordPress database. I tried this script after doing some searching:
DELETE FROM wp_users WHERE ID NOT IN (SELECT DISTINCT post_author FROM wp_105_posts)
but this deleted all the users. Can someone let me know what I am doing wrong please?

Seems like either none of the users made posts, or wp_105_posts.post_author doesn't link to wp_users.ID. Are you sure wp_105_posts.post_author is a Foreign Key of wp_users.ID?
wp_105_posts should have one column that references who the user is in the wp_users table. Since your GUID Primary Key for wp_users is ID, this should be the one column you use to reference the user from your post tables. Here is a small example:
user_table
ID | name | age | email | etc.
1 | nick | 24 | nick#com.com | ..
2 | bob | 30 | bob#com.com | ...
3 | sue | 35 | sue#com.com | ...
a_post_table
ID (ID of post) | User_ID (ID of user) | title | date | body
........1..............|...1 (post by nick)........ | help | 1/1/11
........2..............|...1 (post by nick)........ | help | 3/2/11
........3..............|...2 (post by bob)........ | help | 5/6/11
As you can see, the post table knows everything about the author by simply holding its ID. You can use a join query to get all user info from the post table just by knowing its ID. Using this query now
DELETE FROM user_table
WHERE ID NOT IN
(SELECT DISTINCT User_ID FROM a_post_table)
Will delete Sue, and Sue only, given the data above.
HTH

Related

MySQL compare and update if different

I have 2 tables users and cached_users. Table users is updated and that's my active table. cached_users is my second table and I update that table every 15-20 min.
TABLE USERS
+---------------+---------------+---------------+------------------+
| id | name | username | email |
+---------------+---------------+---------------+------------------+
| 1 | Johne Doe | john.doe | johndoe#mail.com |
+---------------+---------------+---------------+------------------+
TABLE CACHED_USERS
+---------------+---------------+---------------+------------------+
| user_id | name | username | email |
+---------------+---------------+---------------+------------------+
| 1 | Johne Doe | john.doe | johndoe#mail.com |
+---------------+---------------+---------------+------------------+
MY QUESTIOS IS:
How can I check the differences between the two tables and update if something is changed? So I have multiple rows and loop trough all users rows and find matching cached_users row and update if needed. I have more column in cached_users table so I can't do "DELETE/INSERT" method. I need an SQL statement for this action. If you need more information, feel free to ask.
select * from users union select * from CACHED_USERS
if the count is same as users then there is no updation has occured if differs means
select p.id from (select * from users union select * from CACHED_USERS) p group by p.id having count(*)>1
id exist more than one means those records need to be updated.

MySQL link two tables together implicitly

Suppose we have two tables
A table called people with people linked to a bank account balances
| id | name | account_id |
--------------------------
| 1 | bob | 11 |
--------------------------
| 2 | sam | 22
A table called accounts with bank account balances
| id | value |
--------------
| 11 | 200 |
--------------
| 22 | 500 |
In order to link the two tables you can do
SELECT a.value as account_balance
FROM people p
WHERE p.name="bob"
LEFT JOIN accounts a ON p.account_id = a.id`
This would return
id => 1
name => bob
account_balance => 200
That's cool - but I am wondering if there is a more implicit way to do this via SQL linkage (foreign keys or otherwise). Can we in MySQL add links in some other way so that when we do a SELECT, it already knows to return value instead of **account_id **?
I'm asking this because I am creating a system where my users can create lookup tables and link them to other tables - but it must be do-able without any programming. The only other way I can think of is to set the name of account_id for example to accounts.value and treat that as a foreign key when doing a SELECT.
I would have to get the column structure and analyze and then determine that there is a foreign key and then return the appropriate foreign column by looking at the column name.

MySQL database with many tables

I want to create some number of tables (lets say as sub tables) and make all sub tables to point to one main table. If i query with main table I should be able get details from all sub tables together or even from individual subtable table details. I am using python interface for querying.
This is my requirement. Kindly suggest me some ideas. Thanks in advance.
I don't know why are you not using mySQL on terminal to use queries.Why are you using python?
So as per your requirement let us take an example.Let us create a database book_info where there are 4 tables-
tbl_books
tbl_author
tbl_book_reviews
tbl_book_sales
Let tbl_book be the main table.
It will contain-
| book_id | book_title | publication | price | ISBN |
tbl_author will contain-
| author_id | first_name | last_name | DOB | address | email_id |
tbl_book_reviews will contain-
| review_id | book_id | | author_id | Review |
And finally tbl_book_sales will contain-
| book_sale_id | book_id | years | city | quantity
So you can get data from all the tables using Joins as book_id is used as a foreign key in every table.
Try creating tables and executing queries on terminal or mysql Workbench.

Get user's primary email address mysql php best practices

I have a user table and a contact info table so that each user can have multiple phone numbers and multiple email addresses. Is there a standard way to set and retrieve their primary contact info?
"humans" table
ID | name
1 | John
2 | Joan
"human_contact" table
ID | contact_type | contact_info | user_id
1 | email | john#a.com | 1
2 | email | johnny#b.net | 1
"tickets" table
ID | human_id | event_date
1 | 1 | 2017-08-01
if John usually uses johnny#b.net, how do I mark that row as his favorite and retrieve that one for most queries?
SELECT *
FROM tickets
LEFT JOIN humans
ON tickets.human_id=humans.ID
LEFT JOIN human_contact
ON human_contact.human_id=humans.ID
WHERE tickets.ID='112'
You can use an extra coloumn named as fav which will have boolean values. This can help you in first finding whether he has any fav using a check where fav==true, then going on with that contact_info , or if the user doesnt have any favourites, then go with your usual query.
That is,
if( query( fav==1 for atleast 1 tuple )){
return tuple with fav==1;
else{
query ( usual query to get contact_info );
return query_result;
}
for more easier understanding.Hope this helps.

Data Between Two Tables

Excuse any novice jibberish I may use to explain my conundrum but hopefully someone here will be able to look past that and provide me with an answer to get me unstuck.
SESSIONS
+--------+---------+----------+
| id | appID | userID |
+--------+---------+----------+
| 1 | 1 | 96 |
+--------+---------+----------+
| 2 | 2 | 97 |
+--------+---------+----------+
| 3 | 1 | 98 |
+--------+---------+----------+
USERS
+--------+---------+
| id | name |
+--------+---------+
| 96 | Bob |
+--------+---------+
| 97 | Tom |
+--------+---------+
| 98 | Beth |
+--------+---------+
For each session in the Sessions table that has an appID of 1, I want to get the users name from the Users table. The Sessions userID column is linked with the Users tables id column.
So my desired result would be:
["Bob", "Beth"]
Any suggestions/help?
try this:
SELECT USERS.name FROM USERS INNER JOIN SESSIONS ON users.id = SESSIONS.userID WHERE SESSIONS.appID = 1
I would read up on http://blog.codinghorror.com/a-visual-explanation-of-sql-joins/ for how all the joins work.
It looks like you forgot to post your code.
But in explanation.... It seems like you can just select the userID from the sessions table and then simply join the users table. Then create a WHERE clause to select all users that are attached to that ID.
Hope it helps.
If you post your code I can probably help you out more and if this doesnt seem just right lemme know and ill help you how i can
You need to create a join table (http://www.tutorialspoint.com/postgresql/postgresql_using_joins.htm) and then request the data using the equal operator.
SELECT USERS.name FROM USERS, SESSIONS WHERE SESSIONS.userID = USERS.ID ;