how to use join in this case - php

I have two tables:
table pin_info:
id | member_id | look_week | look_name | is_pinned | date
1 | 1 | 3 | the improviser | yes | 2013-11-19 21:57:04
2 | 1 | 2 | destined for stardom | yes | 2013-11-19 21:56:00
3 | 1 | 1 | fashinably corporate | no | 2013-11-19 21:54:00
table arrow_rating:
id | member_id | look_week | look_name | rating |
1 | 1 | 3 | the improviser | 3 |
2 | 1 | 2 | destined for stardom | 4 |
3 | 2 | 1 | fashinably corporate | 5 |
I want is_pinned(from pin_info) and rating(from rating) .I will be having parameter member_id and look_week. (assume 1 and 2 respectively)
What I have done:
SELECT p_i.is_pinned,a_r.rating
FROM pin_info p_i,arrow_rating a_r
WHERE p_i.look_week=a_r.look_week AND p_i.member_id='1'
I am sure this is not the correct way.Any help?

Try this:
SELECT pin_info.is_pinned, arrow_rating
FROM pin_info INNER JOIN arrow_rating
ON pin_info.look_week = arrow_rating.look_week
WHERE pin_info.id = '1';

SELECT is_pinned, rating
FROM pin_info
LEFT JOIN arrow_rating USING (look_week, member_id)
WHERE pin_info.member_id = 1
AND pin_info.look_week = 2
That will select where both member_ids equal 1, and look_week equals 2
The result set for the above is:
is_pinned | rating
------------------
yes | 4

Related

Query Select and Show All First Table And Second Table Even is not Exist in Second Table

SAMPLE TABLE
TABLE FIRST_TABLE
| rid | requirements |
| 1 | 2x2 pic |
| 2 | valid id |
| 3 | 137 form |
| 4 | app form |
Second table
| id | applicant_id | rid | remarks |
| 1 | 1 | 1 | pass |
| 2 | 1 | 2 | pass |
| 3 | 2 | 1 | pass |
How to select all records from first table and show even the data is not exist on second table.
Result should be like this.
applicant_id | rid | remarks |
1 | 1 | pass |
1 | 2 | pass |
1 | 3 | null |
1 | 4 | null |
this is my sample code.
select requirements from first_table
left join second_table on first_table.rid = second_table.rid
where second_table.applicant_id = 1
group by first_table.rid
//result :
applicant_id | rid | remarks |
1 | 1 | pass |
1 | 2 | pass |
You just need to move the second_table.applicant_id = 1 to the join.
select requirements, first_table.rid, remarks
from first_table
left join second_table on
first_table.rid = second_table.rid and
second_table.applicant_id = 1
group by first_table.rid
http://sqlfiddle.com/#!9/a0cffdd/17

Combine table mysql

I got to table need to combine into 1
Table 1 :
| ID | FEEDBACK_VALUE |
| 1 | EMAILS |
| 2 | WALK IN |
| 3 | SMS BLAST |
| 4 | SOCIAL MEDIA |
| 5 | NEWSPAPER |
| 6 | FAMILY & FRIEND |
| 7 | OTHERS |
Table 2 :
| ID | FEEDBACK_ID |
| 1 | 1 |
| 2 | 2 |
| 3 | 2 |
| 4 | 7 |
| 5 | 7 |
| 6 | 7 |
| 7 | 4 |
| 8 | 4 |
| 9 | 3 |
Table 3 :
| ID | FEEDBACK_VALUE | FEEDBACK_RECEIVE |
| 1 | EMAILS | 1 |
| 2 | WALK IN | 2 |
| 3 | SMS BLAST | 1 |
| 4 | SOCIAL MEDIA | 2 |
| 5 | NEWSPAPER | 0 |
| 6 | FAMILY & FRIEND | 0 |
| 7 | OTHERS | 3 |
From table 1 and 2, How can i get result like table 3 using mysql? Thanks
You could use a left jojn, and subquery with count group by
select t1.ID, t1.FEEDBACK_VALUE, ifnull( my_count,0) feedback_receive
from table1 t1
left join (
select FEEDBACK_ID, count(*) as my_count
from table 2
group by FEEDBACK_ID
) t on t1.ID = t.FEEDBACK_ID
Just use a subquery as shown below:
SELECT A.*, (SELECT COUNT(*) FROM TABLE2 B WHERE A.ID=B.FEEDBACK_ID) AS FEEDBACK_RECEIVE
FROM TABLE1 A;
See DEMO on SQL Fiddle
Or, if less code is your thing...
SELECT x.*
, COUNT(y.id) total
FROM table_1 x
LEFT
JOIN table_2 y
ON y.feedback_id = x.id
GROUP
BY x.id;

How to add field in MySQL result?

I want add one filed to result of queries in MySQL.
this tables is my database tables :
tbl_user
id_user | username | password
-----------------------------
1 | Pooria | 123456
2 | mary | 123456
3 | july | 123456
4 | Ali | 123456
5 | shahla | 123456
tbl_category
id_category | name
--------------------
1 | BMW
2 | Toyota
3 | Benz
tbl_content
id_content | message | ContentLike
--------------------------------------
1 | Best Car | 2
2 | Best Car | 3
3 | Best Car | 4
4 | Best Car | 1
5 | Best Car | 1
tbl_user_category_content
id | id_user | id_category | id_content
----------------------------------------
1 | 2 | 2 | 4
1 | 3 | 3 | 3
1 | 4 | 3 | 5
1 | 5 | 1 | 2
tbl_user_like
id_user_like | id_user | id_content
-------------------------------------
1 | 2 | 5
1 | 2 | 3
1 | 5 | 1
1 | 4 | 2
1 | 4 | 2
My Query in Mysql :
SELECT tbl_content.id_content ,tbl_content.message,tbl_content.ContentLike,tbl_category.name,tbl_user.username
FROM tbl_user_category_content
INNER JOIN tbl_user ON
tbl_user_category_content.id_user = tbl_user.id_user
INNER JOIN tbl_cat ON
tbl_user_category_content.id_category = tbl_cat.id_category
INNER JOIN tbl_post ON
tbl_user_category_content.id_content = tbl_content.id_content
and Result
id_content | message | ContentLike | username
----------------------------------------------
1 | Best Car | 2 | Pooria
2 | Best Car | 4 | mary
3 | Best Car | 3 | july
4 | Best Car | 5 | Ali
5 | Best Car | 4 | shahla
I expect Result :
I want insert a id_user and view Result:
example : insert id_user : 2 (mary)
id_content | message | ContentLike | username | Like_User
-------------------------------------------------------------------
1 | hello world1 | 4 | Pooria |
2 | hello world2 | 2 | mary |
3 | hello world3 | 2 | july | Yes
4 | hello world4 | 2 | Ali |
5 | hello world5 | 2 | shahla | Yes
add result Like_User
important:I want all entries in the database with the person who likes to show a distinct field. thanks you for help me <3
Add this in the select columns :
case when tbl_user.id_user=tbl_user_like.id_user then 'Yes' else 'No' end as Like_User
So it would be something like this
SELECT tbl_content.id_content ,tbl_content.message,tbl_content.ContentLike,tbl_category.name,tbl_user.username, case when tbl_user.id_user=tbl_user_like.id_user then 'Yes' else 'No' end as Like_User
FROM tbl_user_category_content
INNER JOIN tbl_user ON
tbl_user_category_content.id_user = tbl_user.id_user
INNER JOIN tbl_cat ON
tbl_user_category_content.id_category = tbl_cat.id_category
INNER JOIN tbl_post ON
tbl_user_category_content.id_content = tbl_content.id_content
I have not test it. Try it out and maybe you should change the column names in the case
Try This Query
SELECT distinct tbl_content.id_content ,tbl_content.message,tbl_content.ContentLike,tbl_category.name,tbl_user.username,tbl_user_category_content.category
FROM tbl_user_category_content
INNER JOIN tbl_user,tbl_cat,tbl_post ON
tbl_user_category_content.id_user = tbl_user.id_user
tbl_user_category_content.id_category = tbl_cat.id_category
tbl_user_category_content.id_content = tbl_content.id_content
WHERE tbl_user.id_user='$user_id'

MYSQL OR two values on one column

I have 2 tables.
Table 1: tA
+----+--------------------+
| id | url |
+----+--------------------+
| 1 | hxxp://www.aaa.com |
| 2 | hxxp://www.bbb.com |
+----+--------------------+
Table 2: tB
+----+-------+--------+----------+
|id | tA_id | cat_id | cat_name |
+----+-------+--------+----------+
| 1 | 1 | 1 | Mobile |
| 2 | 1 | 2 | Other |
| 3 | 2 | 2 | Other |
+----+-------+--------+----------+
Expected Output
+----+-------+--------+----------+
| id | tA_id | cat_id | cat_name |
+----+-------+--------+----------+
| 1 | 1 | 1 | Mobile |
| 3 | 2 | 2 | Other |
+----+-------+--------+----------+
mysql code:
select *
from tA
inner tB
on tA.id = tB.tA_id
where tB.cat_id = 1
or tB.cat_id = 2
Group By tA_id
result from my mysql code:
+----+-------+--------+----------+
| id | tA_id | cat_id | cat_name |
+----+-------+--------+----------+
| 1 | 1 | 2 | Other |
| 3 | 2 | 2 | Other |
+----+-------+--------+----------+
How do I do?
Remove your group by tA_id because it is grouping cat_name Mobile and Other together.
If you need to keep it use ORDER BY tB.cat_id ASC which i believe will show the cat_id 1 and 2 like you need it
There is an error in query... JOIN is missing...
QUERY
SELECT *
FROM tA
INNER JOIN tB
ON tA.id = tB.tA_id
WHERE tB.cat_id = 1
OR tB.cat_id = 2
GROUP BY tA_id
This is fetching expected results
+----+---------------------+-----+--------+--------+----------+
| id | url | id | tA_id | cat_id | cat_name |
+----+---------------------+-----+--------+--------+----------+
| 1 | hxxp://www.aaa.com | 1 | 1 | 1 | mobile |
| 2 | hxxp://www.bbb.com | 3 | 2 | 2 | other |
+----+---------------------+-----+--------+--------+----------+
Try this :-
select *
from tA
inner join tB
on tA.id = tB.tA_id
where tB.cat_id = 1
or tB.cat_id = 2
group by tb.cat_name;
Hope it will help you.

Removing Duplicated Entries: Delete a entry that has 2 the same column

i have this table called bag:
+--------+----------+---------+----------+
| bag_id | chara_id | item_id | item_qty |
+--------+----------+---------+----------+
| 1 | 1 | 2 | 22 |
| 2 | 1 | 1 | 55 |
| 3 | 3 | 1 | 2 |
| 6 | 3 | 4 | 2 |
| 7 | 4 | 4 | 2 |
| 8 | 5 | 4 | 2 |
| 9 | 6 | 4 | 2 |
| 10 | 1 | 5 | 1 |
| 11 | 1 | 2 | 1 |
| 12 | 1 | 2 | 1 |
| 13 | 1 | 2 | 1 |
| 14 | 1 | 8 | 1 |
| 15 | 1 | 6 | 1 |
| 16 | 1 | 8 | 1 |
| 17 | 1 | 6 | 1 |
+--------+----------+---------+----------+
the relationship goes as 1 chara = many item
now i dont want 1 chara = many duplicated item.
how can i make a query that delete's the duplicated values?
like chara_id: 1 has 3 duplicated item_id: 2
i want to delete the other 2.
Not the best way to do it. But the below should definetly work:
Delete from Bag
where bag_id
not in (
select min(bag_id) from bag a,
(select chara_id, item_id
from bag group by chara_id, item_id
having count(*) > 1) b
where a.chara_id = b.chara_id and a.item_id = b.item_id
UNION
select bag_id from bag a,
(select chara_id, item_id
from bag group by chara_id, item_id
having count(*) = 1) b
where a.chara_id = b.chara_id and a.item_id = b.item_id
)
You can simply join table bag with a subquery which gets the minimum bag_id for every combination of chara_ID and item_ID. Records that have null values on any fields on the subquery are the records that will be deleted.
DELETE a
FROM bag a
LEFT JOIN
(
SELECT chara_ID, item_ID, MIN(bag_ID) min_ID
FROM bag
GROUP BY chara_ID, item_ID
) b ON a.bag_ID = b.min_ID AND
a.chara_ID = b.chara_ID AND
a.item_ID = b.item_ID
WHERE b.min_ID IS NULL
SQLFiddle Demo

Categories