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.
Related
I have two tables "contacts" and "users". Users table storing data with "," separated. Need to distinct data in "Contacts" column from "Contacts" table. And need to join with "Users" table, and get the records.
Contacts Table
--------------------------
id | user_Id | contats
--------------------------
1 | 2147483647 | 90123456789,90123456789,90123456789,90123456789
2 | 2147483647 | 90123456789,90123456789,90123456789,90123456789
3 | 919444894154 | 90123456789,90123456789,90123456789,90123456789
Users Table
-----------------------------
id | username | email | phone
-----------------------------
1 | bhavan | bhavanram93#gmail.com | 90123456789
2 | bhavan | bhavanram93#gmail.com | 90123456789
3 | prince | prince#gmail.com | 1234567980
4 | bhavan | bhavanram93#gmail.com | 90123456789
5 | hello | hello#gmail.com | 1234567890
6 | bhavan | bhavanram93#gmail.com | 90123456789
Your table Contacts shouldn't be constructed this way.
Since you want 1 Users table containing all the data about a user, and 1 Contacts table containing links between different users, you'd rather do this kind of table structure :
Contacts table
id | user_id | contact_id
-------------------------
1 | 1 | 2
2 | 1 | 3
3 | 2 | 3
That'll allow you to do something like :
SELECT *
FROM Users
JOIN Contacts ON (Users.id = Contacts.contact_id)
WHERE Contacts.user_id = 1
Which will return all the data of the contacts of the user 1.
Your current structure is a huge ongoing mess, it's the opposite of being flexible.
You should restructure your db to a normalized format as Steve suggest.
But if you cant:
SELECT *
FROM Users
JOIN Contacts
ON CONCAT(',', Contacts.contacts, ',') like
CONCAT('%,', Users.phone, ',%')
WHERE Contacts.user_id = 1
the idea is you convert your contacts to
, <numbers> ,
,90123456789,90123456789,90123456789,90123456789,
and try to match with
%,90123456789,%
Note this approach cant use any index so will have bad performance with many
rows. if you are in the order of 1k-10k rows may be ok. More than that you need consider restructure your db.
I currently have a problem as how to fetch data separately, in the same table, but of different conditions.
To better illustrate, take the example below as my tables.
disputes table
id | user_to | bidder_id
1 | 1 | 2
users table
user_id | user_name
1 | userone
2 | usertwo
I'd like to have an output that combines both like this:
final output table
id | user_to | bidder_id | user_to_name | bidder_id_name
1 | 1 | 2 | userone | usertwo
I do not know how to really put it into words but I hope the illustration helps :
It seeks for the "user_to" and "bidder_id" rows, associates them to the "user_id" in the users table, where it creates two new columns that associates the "user_id" and "bidder_id" to the respective ids in the users table and fetches the user_name in the id given in the field.
LEFT JOIN is your friend. see the exsample:
sample
SELECT d.*,
utn.user_name AS user_to_name ,
bin.user_name AS bidder_id_name
FROM disputes d
LEFT JOIN users utn on utn.user_id = d.user_to
LEFT JOIN users bin on bin.user_id = d.bidder_id;
I have three MySQL tables with many-to-many relationship.
One table is for tasks, another is for users, and the third is for assigning tasks to users (each task can be assigned to several users).
users: tasks: assigned:
+----+------+ +----+-------+ +---------+---------+
| id | name | | id | title | | task_id | user_id |
+----+------+ +----+-------+ +---------+---------+
| 1 | John | | 1 | One | | 1 | 1 |
+----+------+ +----+-------+ +---------+---------+
| 2 | Mike | | 2 | Two | | 1 | 2 |
+----+------+ +----+-------+ +---------+---------+
| 2 | 1 |
+---------+---------+
How do I get tasks assigned to only one user?
How do I get tasks assigned to only one particular user?
You need to join all tables together. Use the following to show all tasks assigned to John:
SELECT name,title
FROM users
JOIN assigned ON (user_id=users.id)
JOIN tasks ON (tasks.id=task_id)
WHERE name="John";
Use GROUP BY and HAVING to see all tasks that were only assigned to one user.
SELECT title
FROM tasks
JOIN assigned ON (task_id=id)
GROUP BY id
HAVING count(*) = 1;
In latter you don't necessarily need to know to who tasks were assigned to, just that they were only assigned to one user. Therefore you don't need to join users table.
Update:
You can use the following to find tasks assigned to John alone:
SELECT name,title
FROM tasks
JOIN assigned ON (task_id=tasks.id)
JOIN users ON (user_id=users.id)
GROUP BY tasks.id
HAVING COUNT(*) = 1 and name="John";
This is possible due to two reasons:
MySQL allows non-aggregated references in HAVING clause.
COUNT(*)=1 forces name to be single value - i.e. you are not in a situation where name would have both 'John' and 'Mike'.
I am trying to write a query which can get invalid refby(is related to id), please check following db structure...
| id | acnumber | refby |
+----+-----------+--------+
| 1 | ac01 | 2 |
+----+-----------+--------+
| 2 | ac02 | 1 |
+----+-----------+--------+
| 3 | ac03 | 5 |
+----+-----------+--------+
As you can find there is no id with value of 5 in above table so query must return 3rd row as result.
I have tried...
SELECT * FROM tbl.members WHERE refby != (SELECT id FROM tbl.members WHERE id = refby)
But this is not giving correct results, please help, thanks.
SELECT * FROM members WHERE refby not in (SELECT id FROM members)
This should solve your problem
You can try this using not in:-
SELECT * FROM tbl.members WHERE refby not in (SELECT id FROM members)
This should be a LEFT JOIN, NOT IN is slow on large tables... assuming id and refid is an PRIMARY KEY or UNIQUE key (read unique within your dataset) then this query should return the same results.
SELECT
*
FROM
members members1
LEFT JOIN
members members2
ON members1.id = members2.refby
WHERE members2.id IS NULL
check the sqlfriddle http://sqlfiddle.com/#!2/05731/1
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