Issue in combining two queries - php

I have an issue on fetching some data. I have 4 tables
user
user_profile
user_income
tax_category
I need to select the tax category applicable to each user. For that i have two queries
The first one select the total income and age of the user and the second one to select the tax category applicable to the user. But i was not been able convert this to one query
SELECT userid,user_profile_gender,date_part('years',age(user_profile_dob))+1 AS userage,incomeresult.totalincome FROM user
LEFT JOIN user_profile ON user_profile_userid = user.userid
INNER JOIN
(SELECT SUM(income_amount) totalincome, income_userid FROM user_income
WHERE income_date BETWEEN '2012-03-30' AND '2014-03-30'
GROUP BY income_userid) AS incomeresult ON user.userid = incomeresult.income_userid
WHERE user.status = 1
SELECT * FROM tax_category
WHERE 70 BETWEEN taxcategory_agefrom AND taxcategory_ageto AND taxcategory_gender=1 AND 12000 BETWEEN taxcategory_incomefrom AND taxcategory_incometo
In the second query 70 should be the value from userage,1 from user_profile_gender and 12000 from incomeresult.totalincome.
Is it possible to create such a query? I am using PostgreSQL 8.4

I guess you could use CTE syntax:
WITH A AS
(
SELECT
userid,
user_profile_gender,
date_part('years',age(user_profile_dob))+1 AS userage,
incomeresult.totalincome
FROM
user
LEFT JOIN user_profile ON user_profile_userid = user.userid
INNER JOIN
(SELECT SUM(income_amount) totalincome, income_userid FROM user_income
WHERE income_date BETWEEN '2012-03-30' AND '2014-03-30'
GROUP BY income_userid) AS incomeresult ON user.userid = incomeresult.income_userid
WHERE
user.status = 1
)
SELECT *
FROM
tax_category
INNER JOIN A
WHERE A.userage BETWEEN taxcategory_agefrom AND taxcategory_ageto AND taxcategory_gender=1 AND A.totalincome BETWEEN taxcategory_incomefrom AND taxcategory_incometo

select
userid,
user_profile_gender,
date_part('years', age(user_profile_dob)) + 1 as userage,
incomeresult.totalincome,
tax_category.*
from
user
left join
user_profile on user_profile_userid = user.userid
inner join
(
select sum(income_amount) totalincome, income_userid
from user_income
where income_date between '2012-03-30' and '2014-03-30'
group by income_userid
) as incomeresult on user.userid = incomeresult.income_userid
left join
tax_category on
date_part('years', age(user_profile_dob)) + 1 between taxcategory_agefrom and taxcategory_ageto
and taxcategory_gender = 1
and totalincome between taxcategory_incomefrom and taxcategory_incometo
where user.status = 1

Related

How do I get friend list from Friends table with counts of friends of my friends

How do I get friend list from Friends table with counts of friends of my friends (Count not of my friends)
Friends table"
tbl_users_friends
Field 1: id
Field 2: user_id
Field 3: friend_user_id
and I need the out put as:
A has following friedns:
x (10)
y (2)
z (0)
Above is the list of my friends and in parenthesis contains their friends count.
Thanks
select user_id, count(*) cnt
from Friends
where user_id in
(select friend_user_id
from Friends
where user_id = user_id_of_A)
group by user_id
Try something like this:
select u.user_id, u.name, count(uf1.id) as num_friends
from tbl_users_friends uf
inner join tbl_users u on u.user_id = uf.friend_user_id
left join tbl_users_friends uf1 on uf1.user_id = uf.friend_user_id
where uf.user_id = 1
group by u.user_id, u.name
http://sqlfiddle.com/#!9/10033/1
You need to ajust the users table and column names.
Another solution with a subselect but without group by:
select u.user_id, u.name, (
select count(*)
from tbl_users_friends uf1
where uf1.user_id = uf.friend_user_id
) as num_friends
from tbl_users_friends uf
inner join tbl_users u on u.user_id = uf.friend_user_id
where uf.user_id = 1

join other tables

SELECT saleamount/(SELECT SUM(saleamount)
from user_pr where user_id=49) *100 as totalPercentage
from user_pr where user_id=49
i also want to join
select users.firstname from users where type=1
how can i combine both in one query
Take the two table names, put JOIN between then and ON and join condition after the second one.
SELECT saleamount/(SELECT SUM(saleamount)
from user_pr where user_id=49) *100 as totalPercentage,
users.firstname
from user_pr JOIN users ON user_pr.user_id = users.user_id
where user_pr.user_id = 49
and users.type = 1

MySQL Query left join repeated records

With a Left Join i have this result.
Here the screen
http://f.cl.ly/items/373Y141r1K131d0n3f1q/Schermata%202013-04-01%20alle%2016.51.18.png
i want to show only record once time, without repeat it, but with a left join all my records are different.
what i have to do for show once all my records?
the query.
SELECT * FROM login_users
LEFT JOIN login_users_seguaci
ON login_users.user_id = login_users_seguaci.following
WHERE name LIKE ""
AND user_id != '1'
ORDER BY data DESC
SELECT x.*, y.*
FROM login_users x
LEFT JOIN
(
SELECT a.*
FROM login_users_seguaci a
INNER JOIN
(
SELECT following, MAX(DATA) max_data
FROM login_users_seguaci
GROUP BY following
) b ON a.following = b.following AND
a.DATA = b.max_date
) y ON x.user_id = y.following
// WHERE ... your condition here ...
ORDER BY t.data DESC

Select and count distinct active users by platform and compare if they exist in other tables

I have about six(6) tables each linked with userid. One of the tables is userinfo. The user info contains user details including their store platform(eg magento)
Userinfo contains both active and non-active users (active users have created at least one activity in the other 5 tables).
I want to count distinct number of users in the userinfo with platform of magento who have records in any of the other tables.
Currently I am able to count distinct number of users in the other five tables with the ff code but want to join this with the userinfo table so I can select active users with platform magento.
Without adding the userinfo table means I have no way of selecting users by platform.
Selecting users in userinfo table only, with platform of magento will be easy, but that means I may select users who only register but do not go on to create activity on my app.
$query3 = ("SELECT COUNT(*)
FROM (
SELECT userid FROM table1
UNION SELECT userid FROM table2
UNION SELECT userid FROM table3
UNION SELECT userid FROM table4
UNION SELECT userid FROM table5
) AS UserIDs");
$result3 = mysql_query($query3) or die(mysql_error());
$row3 = mysql_fetch_row($result3);
echo "Number of distinct users in all tables = ".$row3[0] ."<br />";
**Table 1**
Id userid name adresss
**Table 2**
Id Title Sex userid
**Table 3**
Id userid amount
**Table 4**
Id price promotion userid productid
**Table 5**
Id userid category tax weight
**userinfo**
Id userid username password platform
Expanding on the UNION subselect from my other suggestion, you can JOIN this with the UserInfo table and get your distinct count.
SELECT COUNT (DISTINCT ui.UserID))
FROM (
SELECT UserID FROM Table1
UNION SELECT UserID FROM Table2
UNION SELECT UserID FROM Table3
UNION SELECT UserID FROM Table4
UNION SELECT UserID FROM Table5
) AS id
INNER JOIN UserInfo ui ON ui.UserID = id.UserID
WHERE ui.Platform = 'Magento'
I would like that :
SELECT COUNT(DISTINCT ui.userid) as number
FROM userinfo ui
INNER JOIN table1 t1 ON (t1.userid = ui.userid)
INNER JOIN table2 t2 ON (t2.userid = ui.userid)
INNER JOIN table3 t3 ON (t3.userid = ui.userid)
INNER JOIN table4 t4 ON (t4.userid = ui.userid)
INNER JOIN table5 t5 ON (t5.userid = ui.userid)
WHERE ui.platform = 'magento'
And if you do :
SELECT COUNT(DISTINCT ui.userid) as number
FROM userinfo ui, table1 t1, table2 t2, table3 t3, table4 t4, table5 t5
WHERE ui.platform = 'magento'
AND t1.userid = ui.userid
AND t2.userid = ui.userid
AND t3.userid = ui.userid
AND t4.userid = ui.userid
AND t5.userid = ui.userid
If it doesn't work, try to replace SELECT COUNT(DISTINCT ui.userid) as number by SELECT ui.* for see.

SQL/PHP: SELECT only one row per item

I have a table containing persons information (one row per person) and another table containing persons photos filenames (many rows per person). I want to select a group of persons (based on another table) but only one photo per person.
My old SQL was like this:
SELECT persons.personID, persons.name, persons.photo_filename, movie_cast.role
FROM persons, movie_cast
WHERE persons.personID = movie_cast.personID
AND movie_cast.imdbID = ?
ORDER BY movie_cast.castORDER
LIMIT 9';
But here, the 'persons' table contains also a 'photo_filename' column. In my new database design, this column is in another table. So if I try to get the photo_filename from the new table I get all the photos available for each person, but I need to get only one.
How to do it?
In the first example I have assumed there is always a photo, and am just grabbing the highest sorted photo filename alphabetically as a means to get a consistent photo for each user each time you run the query.
SELECT p.personID, p.name, ph.photo_filename, mc.role
FROM persons p
INNER JOIN movie_cast mc ON p.personID = mc.personID
INNER JOIN (
select personID, max(photo_filename) as MaxPhotoName
from photos
group by personID
) phm on p.personID = phm.personID
INNER JOIN photos ph on phm.personID = ph.personID
and phm.MaxPhotoName = ph.photo_filename
WHERE mc.imdbID = ?
ORDER BY mc.cast
LIMIT 9
If there is a photo_date column and you want to use the newest photo you can do it like this:
SELECT p.personID, p.name, ph.photo_filename, mc.role
FROM persons p
INNER JOIN movie_cast mc ON p.personID = mc.personID
INNER JOIN (
select personID, max(photo_date) as MaxPhotoDate
from photos
group by personID
) phm on p.personID = phm.personID
INNER JOIN photos ph on phm.personID = ph.personID
and phm.MaxPhotoDate = ph.photo_date
WHERE mc.imdbID = ?
ORDER BY mc.cast
LIMIT 9
If there is not always a photo, you can use a LEFT OUTER JOIN so that you will still get all your records back:
SELECT p.personID, p.name, ph.photo_filename, mc.role
FROM persons p
INNER JOIN movie_cast mc ON p.personID = mc.personID
LEFT OUTER JOIN (
select personID, max(photo_date) as MaxPhotoDate
from photos
group by personID
) phm on p.personID = phm.personID
LEFT OUTER JOIN photos ph on phm.personID = ph.personID
and phm.MaxPhotoDate = ph.photo_date
WHERE mc.imdbID = ?
ORDER BY mc.cast
LIMIT 9

Categories