I have two Tables, the table reseau_stream has different information about my a user post. A user can share the post of someone else. Table reseau_share makes that connexion (you have the detail of both table below). Now, if a user share someone else post, I have to order my query using the datetime of reseau_share.
I don't have alot of MySQL skills, but with some help, I finally ended up with the query below. It is working only if reseau_share has a row in it. If reseau_share is empty, the query return 0 result. I really don't understand why. Can anyone identify why ? Cheers.
Table reseau_stream
id user_id content datetime
1 100 Lorem Ipsum1 2013-03-04 19:35:02
2 100 Lorem Ipsum2 2013-03-04 12:35:02
Table reseau_share
id user_id target_id stream_id datetime
-------------------- EMPTY ------------------------
The query
SELECT reseau_stream.id,
reseau_stream.user_id,
reseau_stream.content,
IF(reseau_stream.user_id = 100, reseau_stream.datetime, reseau_share.datetime) as datetime
FROM reseau_stream, reseau_share
WHERE reseau_stream.id
IN (
SELECT id
FROM reseau_stream
WHERE user_id = 100
UNION
SELECT stream_id
FROM reseau_share
WHERE user_id = 100
) ORDER BY datetime DESC;
Basically it looks like you need a LEFT JOIN on reseau_share. Right now you have a FULL OUTER JOIN, which (a) is causing the zero rows as #diegoperini has pointed out and (b) probably isn't what you really want. It's unclear which column relates the two tables. I'll guess it's user_id:
SELECT
reseau_stream.id,
reseau_stream.user_id,
reseau_stream.content,
IF(reseau_stream.user_id = 100, reseau_stream.datetime, reseau_share.datetime) as datetime
FROM reseau_stream
LEFT JOIN reseau_share ON reseau_stream.user_id = reseau_share.user_id
WHERE reseau_stream.id
IN (
SELECT id
FROM reseau_stream
WHERE user_id = 100
UNION
SELECT stream_id -- or whatever
FROM reseau_share
WHERE user_id = 100
)
ORDER BY datetime DESC;
Cartesian product of a non empty set with an empty set is an empty set.
Multiple tables in a FROM statement uses above rule to join two tables which ends up with 0 results in your case.
Related
I have 2 tables (users and usages)
USERS TABLE
username usage
a 32
b 5
c 5
USAGES TABLE
username usage_added
a 7
b 7
c 7
a 30
I want to get all items from USERS table, that have USAGE BIGGER than X (in this case, let's say X is 30) AND if either NO RECORDS are found with the same username in USAGES TABLE or if the usage_added for this username in USAGES TABLE are SMALLER than X (30 in our case)
So in this case, it should return no records. I have a codeigniter query
$this->db->select('users.username');
$this->db->from('users');
$this->db->join('usages', 'usages.username = users.username','left');
$this->db->where("(usages.email is NULL OR (usages.usage_added<30 AND usages.username=users.username))", NULL, FALSE);
$this->db->where("users.usage>30", NULL, FALSE);
By using above query, I still get "username a" returned.
Normally it should not return user A, because user a already has date 30 added. But it seems it compares to first record (a=7) and it says a<30 and it shows it again.
I hope it makes sense and somebody can help.
Written SQL Server syntax, this query should work for you:
DECLARE #usage_limit int = 30;
SELECT A.username
FROM users as A
LEFT OUTER JOIN
(
SELECT username,
usage_added = sum(usage_added)
FROM usages
GROUP BY
username
) as B
ON A.username = B.username
WHERE A.usage > #usage_limit
AND (B.username is null OR B.usage_added < #usage_limit)
This returns no records.
Hope this helps!
You seem to be describing logic like this:
select u.*
from users u
where u.usage > 30 or
not exists (select 1
from usages us
where us.username = u.username and
us.usage > 30
);
You should replace the 30 with a parameter if it varies.
I got a quite complex query (at least for me).
I want to create a list of users that are ready to be paid. There are 2 conditions that need to be met: order status should be 3 and the total should be more then 50. Currently I got this query (generated with Codeingiter active record):
SELECT `services_payments`.`consultant_id`
, `consultant_userdata`.`iban`
, `consultant_userdata`.`kvk`, `consultant_userdata`.`bic`
, `consultant_userdata`.`bankname`
, SUM(`services_payments`.`amount`) AS amount
FROM (`services_payments`)
JOIN `consultant_userdata`
ON `consultant_userdata`.`user_id` = `services_payments`.`consultant_id`
JOIN `services`
ON `services`.`id` = `services_payments`.`service_id`
WHERE `services`.`status` = 3
AND `services_payments`.`paid` = 0
HAVING `amount` > 50
The services_payments table contains the commissions, consultant_userdata contains the userdata and services keeps the order data. The current query only gives me 1 result while I'm expecting 4 results.
Could someone please tell me what I'm doing wrong and what would be the solution?
For ActiveRecord, rsanchez' answer would be more of
$this->db->group_by('services_payments.consultant_id, consultant_userdata.iban, consultant_userdata.kvk, consultant_userdata.bic, consultant_userdata.bankname');
I'm struggling with what should be a simple query.
An event table stores user activity in an application. Each click generates a new event and datetime stamp. I need to show a list of recently accessed records having the most recent datetime stamp. I need to only show the past 7 days of activity.
The table has an auto-increment field (eventID), which corresponds with the date_event field, so it's better to use that for determining the most recent record in the group.
I found that some records are not appearing in my results with the expected most recent datetime. So I stripped my query down the basics:
NOTE that the real-life query does not look at custID. I am including it here to narrow down on the problem.
SELECT
el.eventID,
el.custID,
el.date_event
FROM
event_log el
WHERE
el.custID = 12345 AND
el.userID=987
GROUP BY
el.custID
HAVING
MAX( el.eventID )
This is returned:
eventID custID date_event
346290 12345 2013-06-21 09:58:44
Here's the EXPLAIN
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE el ref userID,custID,Composite custID 5 const 203 Using where
If I change the query to use HAVING MIN, the results don't change.. I should see a different eventID and date_event, as there are dozens of records matching the custID and userID.
SELECT
el.eventID,
el.custID,
el.date_event
FROM
event_log el
WHERE
el.custID = 12345 AND
el.userID=987
GROUP BY
el.custID
HAVING
MIN( el.eventID )
Same results as before:
eventID custID date_event
346290 12345 2013-06-21 09:58:44
No change.
This tells me I have another problem, but I am not seeing what that might be.
Some pointers would be appreciated.
SELECT
el.eventID,
el.custID,
el.date_event
FROM
event_log el
WHERE
el.custID = 12345 AND
el.userID=987 AND
el.eventID IN (SELECT MAX(eventID)
FROM event_log
WHERE custID = 12345
AND userID = 987)
Your query doesn't work because you misunderstand what HAVING does. It evaluates the expression on each line of the result set, and keeps the rows where the expression evaluates to true. The expression MAX(el.eventID) simply returns the maximum event ID selected by the query, it doesn't compare the current row to that event ID.
Another way is:
SELECT
el.eventID,
el.custID,
el.date_event
FROM
event_log el
WHERE
el.custID = 12345 AND
el.userID=987
ORDER BY eventID DESC
LIMIT 1
The more general form that works for multiple custID is:
SELECT el.*
FROM event_log el
JOIN (SELECT custID, max(date_event) maxdate
FROM event_log
WHERE userID = 987
GROUP BY custID) emax
ON el.custID = emax.custID AND el.date_event = emax.maxdate
WHERE el.userID = 987
You can use a group function in a statement containing no GROUP BY clause, but it would be equivalent to grouping on all rows. But I guess you're looking for the common syntax,
SELECT
MIN(el.eventID) AS `min_eventID`, --> Yes it is wrong :(
el.custID,
el.date_event
FROM
event_log el
WHERE
el.userID = 987
GROUP BY el.custID;
But disagreements are welcome .
[ Edit ]
I think I didn't show a solution fast enough... but maybe you're rather looking for the fastest solution.
Assuming field date_event defaults to CURRENT_TIMESTAMP (am I wrong?), ordering by date_event would be a waste of time (and money, thus).
I've made some tests with 20K rows and execution time was about 5ms.
SELECT STRAIGHT_JOIN y.*
FROM ((
SELECT MAX(eventId) as eventId
FROM event_log
WHERE userId = 987 AND custId = 12345
)) AS x
INNER JOIN event_log AS y
USING (eventId);
Maybe (possibly, who knows) you didn't get the straight_join thing; as documented on the scriptures, STRAIGHT_JOINs are similar to JOINs, except that the left table is always read before the right table. Sometimes it's useful.
For your specific situation, we're likely to filter to a certain eventID before (on table "x"), not to retrieve 99,99% useless rows from table "y".
More disagreements expected in 3, 2, ...
Is there a way to create a view from two tables, where one of the columns is different among the two tables? The problem I am currently running into is that MYSQL is telling me that there is an undefined index - which makes perfect sense since, in half of the cases, the column won't exist.
Table Layout:
(post_rank_activity)
ID, post_id, ... date
(reply_rank_activity)
ID, rank_id, ... date
What I want the resulting view to look like:
ID | Post_id | Reply_id | Date
x x NULL x
x NULL x x
And the SQL:
$rankView = "Create or replace view userRank as (
select PRA.id, PRA.post_id, PRA.user_id, PRA.vote_up, PRA.rank_date
From post_rank_activity PRA)
union All
(select RRA.id, RRA.reply_id, RRA.user_id, RRA.vote_up, RRA.rank_date
from reply_rank_activity RRA)";
And the result I'm getting, instead of returning null, it's returning the value of "reply_id" for the "post_id" field and then shifting all of the other values over - see below:
ID | Post_id | Reply_id | Date
x x date val x
x reply val date val x
Any ideas?
Unions must contain the same columns in the same order across all parts. You should explicitly select/declare the null columns in each part of the union:
SELECT PRA.id, PRA.post_id, NULL AS reply_id, PRA.user_id, PRA.vote_up, PRA.rank_date
FROM post_rank_activity PRA
UNION All
SELECT RRA.id, NULL AS post_id, RRA.reply_id, RRA.user_id, RRA.vote_up, RRA.rank_date
FROM reply_rank_activity RRA
Your query should look like
select PRA.id, PRA.post_id, null as Reply_id PRA.rank_date
From post_rank_activity PRA
union All
select RRA.id, null as post_id, RRA.reply_id, RRA.rank_date
from reply_rank_activity RRA
I have three queries that get stats from the database, but the total does not add up correctly for my results. If I do the math myself this is what I get: // 440728 / 1128 = 390.72
However, the following is what is returned by my queries:
SELECT * FROM facebook_accts
WHERE user_id IN (SELECT id FROM `user_accts` WHERE owner_id = '121')
// returns 1128
SELECT sum(friend_count) FROM facebook_accts
WHERE user_id IN
(SELECT id FROM `user_accts` WHERE owner_id = '121')
// returns 440728
SELECT avg(friend_count) FROM facebook_accts
WHERE user_id IN
(SELECT id FROM `user_accts` WHERE owner_id = '121')
// returns 392.11 (number formatted to two decimal places by php)
this may be happening because of column friend_count having some NULL values because SUM and AVG sunctions ignore NULL values. see here.
I guess the 1128 rows contain NULL values (which AVG and SUM ignore).