How to find proper User between two tables - mysql

I have something like this in two tables on my database
"User" table
userId viewsIDS
1 1,2,3,4,5
2 2,4,6,12,13
3 1,4,5
... ...
"View" table
viewID webpageId
1 1
2 1
3 2
4 3
5 3
6 3
... ...
Now, which query should I use to find user IDs just having the webpage IDs?
I guess should somehow group "viewIDs" from "View" table and then check if one of these IDs is in the User "viewIDS" field so take the userID out, but I don't know which query would make this the better way possible.

You can try this:
CREATE TABLE #USER (userId INT, viewsId VARCHAR(50))
CREATE TABLE #VIEW (viewId INT, webPageId INT)
INSERT INTO #USER
SELECT 1, '1,2,3,4,5'
UNION SELECT 2, '2,4,6,12,13'
UNION SELECT 3, '1,4,5'
INSERT INTO #VIEW
SELECT 1,1
UNION SELECT 2,1
UNION SELECT 3,2
UNION SELECT 4,3
UNION SELECT 5,3
UNION SELECT 6,3
SELECT u.userId, v.webPageId FROM #USER u
CROSS APPLY fnSplitString(viewsId, ',') s
INNER JOIN #VIEW v ON s.splitdata = v.viewId
ORDER BY u.userId
I copy this function fnSplitString in:
http://www.sqlservercentral.com/blogs/querying-microsoft-sql-server/2013/09/19/how-to-split-a-string-by-delimited-char-in-sql-server/
But as was told before, you should normalize your table ;)

Related

MySql Select Statement (Other Company)

I am trying to select distinct users that are listed for other companies but not on my company (1). Here is an example
Placement User Company
1 1 1
2 1 2
3 2 2
4 3 1
5 2 1
From this table, I would like to get row 4 since he is in other company (not 1) but listed on others. I do not want others because they are listed on both my company and others. Anyone can help?
You can use NOT IN. For example:
select distinct user
from t
where user not in (
select user from t where company = 1
)
I think that this is the logic that you want:
select t.*
from mytable t
where not exists (
select 1 from mytable t1 where t1.user = t.user and t1.company = 1
)
This gives you the records for which no other record exists for the same user and company 1.

Mysql count(*) based in two relations

I would like to count(*) how much customers have created a post or made a comment. If the same customer has made several posts and comments, it should count only once.
Customer Table:
ID Name ...
1 Jonh
2 Mark
3 King
4 Doe
Post Table:
ID USER_ID...
1 1
2 1
3 3
4 1
Comment Table:
ID USER_ID...
1 1
2 3
3 3
4 4
It should return count(*) = 3
(user_id: 1, 3 and 4).
Try this one. It worked for me and returns what you're looking for:
SELECT COUNT( USER_ID ) AS TOTAL
FROM (
SELECT USER_ID
FROM POSTS
UNION
SELECT USER_ID
FROM COMMENTS
)X
I used POSTS and COMMENTS as table names bc I was unsure what your exact table names are, so make sure to change these in your query.
This should work:
SELECT COUNT(DISTINCT USER_ID) FROM (
SELECT USER_ID FROM POST_TABLE
UNION
SELECT USER_ID FROM COMMENT_TABLE
)

mysql - do not display data if a certain condition is met

Sorry I don't really know how to make a title for this because I can't explain it really. for example i have here a table
c_id emp_id clinic_id
1 1 1
2 1 2
3 2 1
4 3 3
5 1 3
now i will do a query like this
select distinct * from table where clinic_id <> 1
And the result would be
c_id emp_id clinic_id
2 1 2
4 3 3
5 1 3
at this point I need help, if from the where clinic_id <> 1 a certain emp_id is within its row of condition(Sorry for my bad english). for example emp_id 1. All emp_id 1 must not be display also.
So the result would be just
c_id emp_id clinic_id
4 3 3 // *The result I want*
You can use NOT EXISTS for this:
select distinct *
from mytable as t1
where clinic_id <> 1 and
not exists (select 1
from mytable as t2
where t1.emp_id = t2.emp_id and t2.clinic_id = 1)
For the result you're looking for, wouldn't something like this be simpler?
SELECT DISTINCT * FROM table WHERE clinic_id !=1 AND emp_id !=1
Here we're saying we want any clinic_id that is not 1 and any emp_id that is also not 1.
Since you're dealing with PHP, then you would simply substitute the numbers for the variables you're trying to not match.
SELECT DISTINCT * FROM table WHERE clinic_id !=$session_variable AND emp_id !=$some_value
SELECT DISTINCT *
FROM table
WHERE clinic_id <> 1
AND emp_id NOT IN
(SELECT DISTINCT emp_id
FROM table
WHERE clinic_id = 1)
Try this one.
It uses the subquery to return the emp_ids which are in the same row as the 1 in the column clinic_id, and removes them from the resultset because you also don't want those emo_ids.
Also you could use a GROUP BY clause instead of DISTINCT. Usually GROUP BY would be turned into a distinct by the database if you are not using any aggregate functions, but sometimes they behave differently. If you are interested in this topic you can also see this question: Is there any difference between GROUP BY and DISTINCT
SELECT c_id, emp_id, clinic_id
FROM clinics
WHERE clinic_id <> 1
AND emp_id NOT IN
(SELECT DISTINCT emp_id
FROM clinics
WHERE clinic_id = 1)
GROUP BY c_id, emp_id, clinic_id;

Mysql Query to Merge 3 tables into 1 Table

TO Plot Each Input table I have Separate query, need to apply functionality on that queries and want to create Single query for Output Table
Select Distinct Names, SUM(count) from
(Select Query table 1
union
Select Query table 2
union
Select Query table 3) table group by Names;
this query Not adding count properly Niether Sorting Names properly Whats wrong with this ?
Input Table 1 :-
Names count
bob 3
pol 4
Input Table 2 :-
Names count
bob 5
0 - name may be missing here neglect this entry
Input Table 3 :-
Names count
james 4
pol 7
bob 1
Expected output table :-
Names count
bob 9
pol 11
james 4
You can use UNION and them sum of those.
select sum(a), sum(b) from
(select 2 as a, 1 as b
union select 3 as a, 6 as b
union select 4 as a, 1 as b) as b
Try this query
select `Name`,sum(`Count`) total from ( select `Name`,`Count` from `table1` union all select `Name`,`Count` from `table2` union all select `Name`,`Count` from `table3` ) tot group by `Name`
May this help you.

How to create table with a column computed by another table in SQL Server?

How to create table with a column computed by another table in SQL Server?
For example:
TableA:
Name SerialNoStart SerialNoEnd
A 1 3
B 2 4
C 1 1
I want create a new table with serial number between SerialNoEnd and SerialNoStart
The new table as following:
TableB:
Name SerialNo
A 1
A 2
A 3
B 2
B 3
B 4
C 1
How to make it? Thanks!
You can split the data out using a recursive CTE, similar to this:
;with data(name, SerialNoStart, SerialNoEnd) as
(
select name, SerialNoStart, SerialNoEnd
from TableA
union all
select name, SerialNoStart +1, SerialNoEnd
from data
where SerialNoStart +1 <= SerialNoEnd
)
select name, SerialNoStart as SerialNo
from data
order by name
See SQL Fiddle with Demo