I have two tables post and share, post has many share. I want to fetch all data in post table using userId(posted by owner user) and also check share table using same userid that is if some one shared post to other user, if any condition is true, I need to fetch data.
I want to fetch data in post table if posted by owner or shared by other user in share table.
Example :
table name: post
id(pk) postname userid
1 abc 10
2 xxx 10
3 yyy 11
4 zzz 12
5 bbb 13
table name:share
id postid(fk) userid
1 3 10
2 4 10
3 3 11
4 1 12
Expected output: example find by userid 10
id postname userid
1 abc 10 // this record created by user 10 (owner)
2 xxx 10 // this record created by user 10 (owner)
3 yyy 11 // this record shared by other user to user 10.
4 zzz 12 // this record shared by other user to user 10.
You may want to print created_by and shared_by in two different columns as the example seems a bit confusing. Below query should produce the expected output in that case:
select p.id, p.postname, p.userid as 'created_by', s.user_id as 'shared_by'
from post p left outer join share s on p.id = s.postid
where p.userid = 10
order by p.id;
It finally clicked what you are looking for..
select p.id, p.postname, p.userid
from post p
join share s on s.postid=p.id
where s.userid='10' or p.userid='10'
That should return all interactions related to a specific user (id 10), either if a post was created by that user, or a post was shared to that user.
Related
There're two tables in database
table users with some datas
username invitedby
123 23
12 45
89433 11893
faf 123
afsafgf 12
table list
lname type
I want to import data from users to list, and the list data show like this
lname type
123 3
12 3
89433 3
faf 3
afsafgf 3
123 2
12 2
First, import all datas into users, and lname is from username, and set the type to be 3.
Second, if some username inivited another, such as faf is invited by 123, afsafgf is invited by 12, insert the inviter and set its type to be 2, the last two line data above.
How can I write this SQL query in mysql? Many thanks.
Is this what you want?
insert into list (lname, type)
select username, 3
from users
union all
select invitedby, 2
from users u
where exists (select 1 from users u2 where u2.username = u.invitedby);
I have multiple checkboxes where user can add education data to database. My structure looks following in database: I have three tables, user_education where I keep the record users selected education levels (it can have multiple rows for the same user_id), education, where I store user education description (only one row per user_id), and education levels where I store all the degrees.
user_education
id | user_id | education_id
1 83 1
2 83 2
education_description
user_id | description
83 test
education_levels
education_id | education
1 alusharidus
2 keskharidus
3 Bachelor’s degree
4 Master’s degree
and so on. My question is how can I select all the data for the specific user, but only show the description once?
I tried running this:
select
user_education.education_id,
education_levels.education,
education_description.description
from
user_education
join education_description
left join education_levels
on user_education.education_id=education_levels.education_id and education_description.user_id = user_education.user_id WHERE user_education.user_id=83;
which gave me this:
and here is the description inserted multiple times, but I would like to only have it once. Is my structure wrong for this type of logic? If not, how can I select it only once, so I can append the data for the user after he submits the form?
EDIT
adding desired result
education_description
user_id | education | description
83 test testing
83 test2
83 test3
Can you please try this answer :-
select
user_education.education_id,
GROUP_CONCAT(education_levels.education separator ' ,'),
education_description.description
from
user_education
join education_description
left join education_levels
on user_education.education_id=education_levels.education_id and education_description.user_id = user_education.user_id
WHERE user_education.user_id=83
GROUP BY user_education.user_id;
I have the following table
id Desc User
1 Print 14
2 Print 7
3 Copy 14
4 Print 19
5 Copy 7
6 Copy 19
7 Attach 19
What I'm trying to do is make a column that tells the number of rows per user.
Like this
id User Count
1 14 2
2 7 2
4 19 3
The Point of the report is to show how many activities each user has done.
I need to group by user and get the number of rows within each user.
The problem is, I'm not exactly sure how to do that, is it a unique statement somewhere?
Here's my query so far.
Select id
,User
From Table
Group By User
I am unsure how to implement the count though.
You should be able to get your result with the COUNT function:
SELECT
MIN(id),
User,
COUNT(User) AS Count
FROM
`table`
GROUP BY
User
Because you can get only one id value per User I assumed from your data that you want the minimum one.
I have a database with tours (group city tours with guide). What I need is a SQL query (for a mysql 5.1.54 database) that will eventually give me a PHP array with the information out of multiple tables combined. I'm able to accomplish this by doing a query just for the first 3 tables and then adding the information in table 4 and 5 within a foreach loop.
But, I need to use the query for searching/filtering (like: show all reservations where partner has id 9) so I need SQL to select the corresponding reservations.
Reservations with no partners (table 4) and/or guides (table 5) must be included.
All partners and guides must be included in the result (reservation can have more partners/guides)
All information in table 4 and 5 must be included (like the status from table 4).
A simplified version of the database:
Table 1: reservations:
id id_client id_tour
1 22 6
2 23 5
Table 2: clients (one reservation has one client):
id name
22 John
23 William
Table 3: tours (one reservation has one tour)
id name
5 big tour
6 small tour
Table 4: partners (one reservation can have multiple partners):
id id_reservation id_partner_type id_partner status
34 1 9 16 1
35 1 9 17 0
Table 5: guides (one reservation can have multiple guides):
id id_reservation id_guide
18 1 14
19 1 15
I have tried to work this out but I just can not get a query that does the job. I used GROUP_CONCAT to get the multiple partner and guide id's. Biggest problem I have is not being able to include the reservations that have no partners and/or guides like reservation 2.
To include all reservations that have no partners and guides you need to use OUTER JOINs, for example the following query will gives you all information from your tables 4, 5 including your condition:
Select p.*, g.*
from reservations r
left outer join partners p on p.id_reservation = r.id
left outer join guides g on g.id_reservation = r.id
where p.id_reservation is null and g.id_reservation is null
Not sure how to ask this so it makes sense, but I'm trying to do a query like:
SELECT * FROM PAGES WHERE an attached record exists in PAGE_FILTERS and that record has a FilterTypeID of 22 AND another attached record exists in PAGE_FILTERS for the same page ID and that record has a filter type id of 27.
I have a structure like this:
PAGES table
PageID PageName
1 Page 1
2 Page 2
3 Page 3
PAGE_FILTERS table
PageID FilterTypeID FilterValueID
1 22 1
1 27 2
2 22 0
2 24 1
3 22 1
3 27 1
3 28 2
So, given FilterTypeID's of 22 and 27, my query should return PageID's 1 and 3. Page 2 doesn't get selected since 22 matches, but there's no record matching FilterTypeID 27.
Page 3 matches, even though there's an extra filter.
In other words, I know what filter types a page has to have, and I need to get all the pages that each have all the required types.
I'm not opposed to changing database structure if it makes more sense, but each page could have none, one, or many filter sets attached.
SELECT p.*
FROM pages p
JOIN PAGE_FILTERS pf
ON p.PageID = pf.PageID
AND pf.FilterTypeID IN (22,27)
GROUP BY p.PageID
HAVING COUNT(DISTINCT pf.FilterTypeID) = 2