How to add field in MySQL result? - php

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'

Related

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 find displaying data where data not exist in the other table

I have 3 tables:
table_1:
| id | test |total_employee|
+----+------+--------------+
| 1 | xxxx | 3 |
| 2 | yyyy | 2 |
| 3 | zzzz | 3 |
----------------------------
table_2:
| id | id_table1 |id_employee|
+----+-----------+-----------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 1 | 3 |
| 4 | 1 | 1 |
| 5 | 1 | 2 |
| 6 | 1 | 1 |
| 7 | 1 | 2 |
| 8 | 1 | 3 |
-----------------------------
table employee:
| id | name |
+----+------+
| 1 | emp1 |
| 2 | emp2 |
| 3 | emp3 |
-------------
and now I want to display all employees from table employee and from table_2 if the employee record in table employee exists but in table_2 it does not. If record does not exist mark as "unchecked" but if exists mark as "checked".
The point is how to find data from table_employee no matter data is exist or not exist on table_2.
The IF() operation lets you do this:
SELECT e.*, IF(ISNULL(t2.id), 'unchecked', 'checked') AS `checked`
FROM table_employee e
LEFT JOIN table_2 t2 ON t2.id_employee = e.id

how to use join in this case

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

select data from database Mysql query

tbl_pack_service;
+-------+---------+------------+
| ps_id | pack_id | service_id |
+-------+---------+------------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 2 | 1 |
| 4 | 2 | 2 |
| 5 | 2 | 3 |
| 6 | 2 | 4 |
| 7 | 2 | 5 |
| 8 | 3 | 1 |
| 9 | 3 | 2 |
| 10 | 3 | 3 |
| 11 | 3 | 4 |
+-------+---------+------------+
ps_id is primary key
i am tying to make dynamic select list in php
i want a mysql query that can give service id which do not match with particular pack_id
like i have service_id 1,2,3,4,5
when i should select pack_id=1 then 3,4,5 should be displayed
and when i should select pack_id=2 then nothing should be displayed as it has all the 5 services.
thanks..
There are a few ways to handle this. The easiest is with a NOT IN subquery:
SELECT DISTINCT service_id
FROM
tbl_pack_service
WHERE service_id NOT IN (SELECT service_id FROM tbl_pack_service WHERE pack_id = 1)

SQL statement to show difference between every user choice and one user choice

Basically I need to create output TOP table where users are arranged by comparing their points with admin's points.
For example:
User3 | 0 //Everything was as admin had.
User5 | 3 //One song had 2 points different from admin and one was off by one
ect.
In my database I have three tables:
Table: rating
+------------+---------+----------+---------+
| rating_id | user_id | song_id | points |
+------------+---------+----------+---------+
| 1 | 1 | 4 | 0 |
| 2 | 1 | 3 | 1 |
| 3 | 3 | 2 | 3 |
| 4 | 4 | 2 | 2 |
| 5 | 2 | 1 | 4 |
Table: songs
+---------------+------------+
| song_name_id | song_name |
+---------------+------------+
| 1 | Song1 |
| 2 | Song2 |
| 3 | Song3 |
| 4 | Song4 |
| 5 | Song5 |
Table: users
+----------+----------+----------+
| id | username | password |
+----------+----------+----------+
| 1 | User1 | passw |
| 2 | User2 | wordp |
| 3 | User3 | somet |
| 4 | User4 | hings |
It should be something like this (not in any programming language):
Compare user_id > 1 with user_id=1 //Let's say that the comparable admin is user_id=1
$result= ABS(user.points-admin.points)++;
And put this to array as:
username => result
Then when I sort this array by result, I can print it as top table - who got the closest result to admin!
I tryed several different solutions but never got the right result.
Can anybody help me?
UPDATE:
Thanks!
With JOIN the result is:
+------------+---------+----------+---------+-----------+
| song_id |song_name| user_id |username |rating_diff|
+------------+---------+----------+---------+-----------+
| 1 | Song1 | 1 | admin | 0 |
| 2 | Song2 | 1 | admin | 0 |
...etc...
With LEFT JOIN the result is:
+------------+---------+----------+---------+-----------+
| song_id |song_name| user_id |username |rating_diff|
+------------+---------+----------+---------+-----------+
| 1 | Song1 | 11 | user2 | NULL |
| 1 | Song1 | 10 | user1 | NULL |
| 1 | Song1 | 12 | user3 | NULL |
| 1 | Song1 | 1 | admin | 0 |
| 2 | Song2 | 11 | user2 | NULL |
| 2 | Song2 | 10 | user1 | NULL |
| 2 | Song2 | 12 | user3 | NULL |
| 2 | Song2 | 1 | admin | 0 |
..etc..
So.. Something is wrong, the rating_diff does not work.
Assuming you want this comparison on a song-by-song basis (instead of a total or average across all songs), try:
select r.song_id,
s.song_name,
r.user_id,
u.username,
abs(r.points - r1.points) rating_diff
from rating r
join songs s on r.song_id = s.song_name_id
join users u on r.user_id = u.id
join rating r1 on r.song_id = r1.song_id and r1.user_id = 1
order by s.song_name, abs(r.points - r1.points)
This should sort the output by the song name, and then by the difference between the admin's points and the users' points. (Change the join on rating r1 to be a left join if you can't guarantee an admin rating for every song.)

Categories