I'm stuck in this MySQL query. I get way too many results.
SELECT
un.user_uid,
un.notification_date,
un.notification_text,
un.notification_type,
un.post_id,
up.user_uid AS notification_user_uid,
uu.user_username
FROM user_notifications un
LEFT JOIN
user_posts_comments up
ON
up.post_id = un.post_id
LEFT JOIN
user_user uu
ON
up.user_uid = uu.user_uid
WHERE
un.notification_status = 1 AND
un.user_uid = '2536'
ORDER BY un.notification_date DESC
I have a table UN that contains the post_id, I look in the UP for the same post_id and get the user_uid, than I look in the UU table for the user_username.
But this way I get duplicate results (I know why but don't find how I can resolve it.
This is the result I need:
un.user_uid
un.notification_date
un.notification_text
un.notification_type
un.post_id
notification_user_id
uu.user_username
2536
2023-02-01
Text 1
1
AZ325B
2530
John
2536
2023-02-01
Text 2
1
BZ826C
2530
John
2536
2023-02-01
Text 3
1
CC724E
2489
Sarah
2536
2023-02-01
Text 4
1
AB234F
2358
Beth
These are the tables I have:
user_notifications
un.user_uid
un.notification_date
un.notification_text
un.notification_type
un.post_id
2536
2023-02-01
Text 1
1
AZ325B
2536
2023-02-02
Text 2
1
BZ826C
2536
2023-02-03
Text 3
1
CC724E
2536
2023-02-04
Text 4
1
AB234F
2489
2023-02-05
Text 5
1
ZA256R
2489
2023-02-06
Text 6
1
TR856D
2580
2023-02-07
Text 7
1
AQ156R
2596
2023-02-08
Text 8
1
BE895V
2573
2023-02-09
Text 9
1
UY853Z
user_posts_comments
un.post_id
un.post_text
AZ325B
Hello this is post 1
BZ826C
Hello this is post 2
CC724E
Hello this is post 3
AB234F
Hello this is post 4
ZA256R
Hello this is post 5
TR856D
Hello this is post 6
AQ156R
Hello this is post 7
BE895V
Hello this is post 8
UY853Z
Hello this is post 9
user_user
uu.uid
uu.user_username
2530
John
2358
Beth
2489
Sarah
2536
Tom
2573
David
You could use inner join instead of left join. I guess that way you'd get only overlapping tuples between tables.
Related
I probably haven't explained this very well in the title but I have two tables. Here is a simple version.
channel_data
entry_id channel_id first_name last_name model other_fields
1 4 John Smith
2 4 Jane Doe
3 4 Bill Evans
4 15 235
5 15 765
6 15 543
7 15 723
8 15 354
9 15 976
10 1 xxx
11 2 yyy
12 3 123
channel_titles
entry_id author_id channel_id
1 101 4
2 102 4
3 103 4
4 101 15
5 101 15
6 101 15
7 102 15
8 102 15
9 103 15
10 101 1
11 102 2
12 103 3
I am not able to re-model the data unfortunately.
I need to list all the rows with a channel_id 15 from channel_data and beside them the first_name and last_name which has the same author_id from channel_titles.
What I want to return is this:
Model First Name Last Name
---------------------------------
235 John Smith
765 John Smith
543 John Smith
723 Jane Doe
354 Jane Doe
976 Bill Evans
If Model was in one table and Names were in another this would be much simpler but I'm not sure how to go about this when they are in the same table.
========================================
Edited to clarify.
I need to get each model with a channel_id 15 from channel_data
For each model I need to look up the entry_id in channel_titles to find the author_id
I need to find the row with that author_id AND channel_id 4 in channel titles (each row with channel_id 4 has a unique author_id).
I need to take the entry_id of this row back to channel_data and get the first_name and last_name to go with the model.
I am well aware that the data is not structured well but that is what I have to work with. I am trying to accomplish a very small task in a much larger system, remodelling the data is not an option at this point.
I think sub-queries might be what I am looking for but this is not my area at all usually.
Ok, that is convoluted. However, based on your description, this query should give you the results you want. The WHERE and JOIN descriptions follow the logic you have described in your question.
SELECT cd1.model, cd2.first_name, cd2.last_name
FROM channel_data cd1
JOIN channel_titles ct1 ON ct1.entry_id = cd1.entry_id
JOIN channel_titles ct2 ON ct2.channel_id = 4 AND ct2.author_id = ct1.author_id
JOIN channel_data cd2 ON cd2.entry_id = ct2.entry_id
WHERE cd1.channel_id = 15
ORDER BY cd1.entry_id
Output:
model first_name last_name
235 John Smith
765 John Smith
543 John Smith
723 Jane Doe
354 Jane Doe
976 Bill Evans
Demo on SQLFiddle
I have two tables. I want to combine them but include rows that don't return a value. This question is probably going to be flagged as duplicate or something, but I have already tried reading the other posts and still failed. So I might be below the average MySQL Programmer. Hope somebody can help.
table_price_list
item_id price_type_id price_amount
1 1 100.00
1 2 95.00
1 3 90.00
1 4 85.00
1 5 80.00
1 6 75.00
2 1 201.56
2 2 196.45
2 3 191.78
2 4 186.36
3 1 1210.12
3 2 1205.45
3 3 1200.69
3 4 1195.48
3 5 1190.98
table_price_type
price_type_id price_type
1 srp
2 reseller
3 distributor
4 mega
5 depot
6 special
Desired output
item_id price_type_id price_type
1 srp 100.00
1 reseller 95.00
1 distributor 90.00
1 mega 85.00
1 depot 80.00
1 special 75.00
2 srp 201.56
2 reseller 196.45
2 distributor 191.78
2 mega 186.36
2 depot null
2 special null
3 srp 1210.12
3 reseller 1205.45
3 distributor 1200.69
3 mega 1195.48
3 depot 1190.98
3 special null
The best I could get so far is this, this leaves out the blank price_type
select b.item_id, a.price_type, b.price_amount
from table_price_type A
left outer join table_price_list B on A.price_type_id=B.price_type_id
It doesn't have to be null, it could just be blank (' ').
You can use left join to get required result.
select b.item_id, a.price_type, b.price_amount
from table_price_type A
left join table_price_list B on A.price_type_id=B.price_type_id
I have two tables. One is ps_product_lang and ps_category_product. The data inside ps_category_product is like this
id_category id_product
2 1
2 2
2 3
2 4
2 5
2 6
2 7
3 1
3 2
3 3
3 4
3 5
3 6
3 7
4 1
4 2
5 1
7 2
8 3
8 4
8 5
8 6
8 7
9 3
10 4
The table for ps_product_lang is like this
id_product id_lang name
1 1 Faded Short Sleeves T-shirt
1 2 Faded Short Sleeves T-shirt
2 1 Blouse
2 2 Blouse
3 1 Printed Dress
3 2 Printed Dress
4 1 Printed Dress
4 2 Printed Dress
5 1 Printed Summer Dress
5 2 Printed Summer Dress
6 1 Printed Summer Dress
6 2 Printed Summer Dress
7 1 Printed Chiffon Dress
7 2 Printed Chiffon Dress
So here I want to get the id_product,name from ps_product_lang where id_category is 5,7 and name like 'p%'. so can someone tell me what would be the query. Any help and suggestions would be really appreceable.
As #Gordon was hinting, you simply need an INNER JOIN between the two tables, along with a where condition to restrict the product category and name:
SELECT t1.id_product,
t1.name
FROM ps_product_lang t1
INNER JOIN
(
SELECT DISTINCT id_product
FROM ps_category_product
WHERE id_category IN (5, 7)
) t2
ON t1.id_product = t2.id_product
WHERE t1.name LIKE 'P%'
Here is a demo which omits the WHERE clause to show how the query behaves when it actually returns data:
SQLFiddle
Use JOIN to get data from 2 tables
SELECT p.id_product,name
FROM ps_product_lang p
JOIN ps_category_product c
ON p.id_product=c.id_product
WHERE id_category IN (5,7)
AND name LIKE 'p%'
I have a problem with sql query. I need join three tables, count rows, get last row, and the create custom column.
The example my tables like below:
table name: article
_______________________________________
idarticle idwriter title
---------------------------------------
1 1 Title One
2 3 Title Two
3 2 Title Three
table name: comment
________________________________________________________________________________
idcomment idarticle idcommented content datetime
--------------------------------------------------------------------------------
1 1 2 Comment One 2015-05-15 00:00:00
2 1 3 Comment Two 2015-05-16 00:00:00
3 1 1 Comment Three 2015-05-17 00:00:00
4 2 2 Comment Four 2015-05-18 00:00:00
5 3 3 Comment Five 2015-05-19 00:00:00
6 3 2 Comment Six 2015-05-20 00:00:00
table name: member
_____________________
idmember username
---------------------
1 apple
2 orange
3 banana
How to join all tables, count, and get last commented+username with one query.
May the result like:
_____________________________________________________________________________________________________________________
idarticle idwriter title username_writer totalcomments lastcomment_id lastcomment_username lastcomment_datetime
---------------------------------------------------------------------------------------------------------------------
3 2 Title Three orange 2 2 orange 2015-05-20 00:00:00
2 3 Title Two banana 1 2 orange 2015-05-18 00:00:00
1 1 Title One apple 3 1 apple 2015-05-17 00:00:00
I hope someone will solve my problem.
I using PHP 5.4, MySQL 5.5, and MeekroDB library, so the query must support with those.
Sorry for my English.
Thank you
http://sqlfiddle.com/#!9/132336/1
SELECT a.idarticle, a.idwriter , a.title,
m.username,
c.idcomment, cm.username, c.datetime
FROM article a
LEFT JOIN member m
ON a.idwriter = m.idmember
LEFT JOIN comment c
ON a.idarticle = c.idarticle
LEFT JOIN comment c1
ON a.idarticle = c1.idarticle
AND c.datetime<c1.datetime
LEFT JOIN member cm
ON c.idcommented = cm.idmember
WHERE c1.datetime IS NULL
i have 2 tables:
players
id | dni | name
-----------------
1 222 mike
2 333 gerard
3 444 mark
4 555 alfred
5 666 thomas
5 777 nicolas
teams
id | dni1 | dni2 | cat
------------------------
1 222 333 1
2 444 555 1
3 666 333 2
4 777 222 2
what i want is to make a select statement depending on the cat. so for
cat=1 should be:
mike gerard
mark alfred
and for cat=2 should be:
thomas gerard
nicolas mike
i tryed many join statements but i cant figure it out, can you help me please?
EDIT to make it more difficult, the players table have a new colum= points
players
id | dni | name | points
--------------------------
1 222 mike 1
2 333 gerard 2
3 444 mark 3
4 555 alfred 4
5 666 thomas 5
5 777 nicolas 6
now the result give us the sum of the points colums from both players and order by biggest sum number.
cat=1 should be:
mark alfred 7
mike gerard 3
select t.cat, p1.name name1, p2.name name2, p1.points + p2.points points
from teams AS t
join players AS p1 ON p1.dni = t.dni1
join players AS p2 on p2.dni = t.dni2
where cat = 1
order by points desc
DEMO