I have this query:
SELECT `jos_eb_registrants`. * , `jos_eb_field_values`. *
FROM jos_eb_registrants, jos_eb_field_values
WHERE `jos_eb_registrants`.`event_id` =3
AND `jos_eb_registrants`.`id` = `jos_eb_field_values`.`registrant_id`
It returns me everything from these two tables, but gives me rows with duplicate names, because ONE name from first table can have more than one field value. How to make field values not in separate rows, but in one row near one name, without duplicates. Please edit this query with my variables.Tables connected via Id and registrant_id.
jos_eb_field_values
1 id
2 registrant_id
3 field_id
4 field_value
jos_eb_registrants
1 id
2 event_id
3 user_id
4 group_id
5 first_name
6 last_name
7 organization
8 address
9 address2
10 city
11 state
12 country
13 zip
14 phone
15 fax
16 email
17 number_registrants
18 total_amount
19 discount_amount
20 amount
21 register_date
22 payment_date
23 payment_method
24 transaction_id
25 comment text
26 published
27 cart_id int(11)
28 deposit_amount
29 payment_status
30 coupon_id
31 check_coupon
32 tax_amount
33 registration_code
34 is_reminder_sent
35 is_group_billing
Try this:
SELECT DISTINCT jos_eb_registrants. * , jos_eb_field_values. *
FROM jos_eb_registrants
INNER JOIN jos_eb_field_values ON jos_eb_registrants.id = jos_eb_field_values.registrant_id
WHERE jos_eb_registrants.event_id =3
Related
I have a table "Payments" where student_id is a foreign key.
student_id Monthpaid year payment_id
51 1 2019 1
52 1 2019 2
51 2 2019 3
I tried this query:
SELECT * FROM Payments WHERE Monthpaid !=2
I get this result:
student_id Monthpaid year payment_id
51 1 2019 1
52 1 2019 2
51 2 2019 3
But I want this where ID 52 doesn't have Monthpaid = 2:
student_id Monthpaid year payment_id
51 1 2019 1
I think you ware looking for:
select t.*
from t
where not exists (select 1
from t t2
where t2.id = t.id and t2.monthpaid = 2
);
This returns all rows for an id where that id does not have a month of 2. That is how I interpret your question.
I want to join one to many table with single row on many table by limit 1 and order by create date
tbl_cart :
id fullname
1 myname1
2 myname2
3 myname3
tbl_cart_status:
id cart_id status created_at
1 1 33 2018-09-20
2 1 34 2018-09-23
3 2 34 2018-09-21
4 1 100 2018-09-25
5 2 35 2018-09-29
How can i get output with sql like this:
I want to get lastest status of my cart by ordered with created_at column
myname cart_id status created_at
myname1 1 100 2018-09-25
myname2 2 35 2018-09-29
Think filtering for this type of query:
select c.name, cs.*
from tbl_cart c join
tbl_cart_status cs
on c.id = cs.cart_id
where cs.created_at = (select max(cs2.created_at)
from tbl_cart_status cs2
where cs2.cart_id = cs.cart_id
);
I have the following MySQL table
1 id mediumint(9)
2 user_id mediumint(9)
3 location_id mediumint(9)
4 timestamp timestamp
5 activity varchar(100)
6 points mediumint(9)
I want to query user_id, location_id, date, SUM(points) between (grouped by) 2 different activities (checkin and checkout), but i can't figure out how.
Any activity between checkin and checkout should be allowed.
1 38 13 2015-05-13 13:26:01 checkin 100
2 38 13 2015-05-13 13:26:07 add_points 5
3 38 13 2015-05-13 13:26:13 add_points 5
4 38 13 2015-05-13 13:26:19 add_points 5
5 38 13 2015-05-13 13:26:26 add_points 5
6 38 13 2015-05-13 13:26:31 add_points 5
7 38 13 2015-05-13 13:26:37 add_points 5
8 38 13 2015-05-13 13:33:42 checkout 0
the result for this set should be:
userid location date sum of points between checkin and checkout
38 13 2015-05-13 130
when there are more checkin and checkouts, the result should add another row.
http://sqlfiddle.com/#!9/ba90b/2
SELECT t.*,
SUM(t.points)
FROM (
SELECT *,
IF(activity='checkin', #gr:=CONCAT(user_id,`timestamp`), #gr) gr,
IF(activity='checkout', #gr:=null, #gr)
FROM table1
ORDER BY user_id,`timestamp`) t
GROUP BY t.gr
I guess this would do, because your also summarizing the points from checkin (100) and checkout (0).
I assumed that userid and location are a unique combination else you have to work with date indeed.
SELECT userid
, location
, DATE_FORMAT(date,'%m-%d-%Y')
, sum(points) total_points
FROM table
GROUP BY userid
, location
, DATE_FORMAT(date,'%m-%d-%Y');
If there are multiple checkin / checkouts with the same userid and location then you can consider to add a column like session to your table or an other unique identifier. It makes it more easier to query. You can use that identifier in your GROUP BY to get the multiple records you would like to have.
I'm trying to search by three conditions under Wordpress wp_usermeta table which is like below:
umeta_id user_id meta_key meta_value
49 3 email schodde.virginia#saugov.sa.gov.au
51 3 suburb Noarlunga
52 3 state SA
53 3 postcode 5168
54 3 maternitybg Yes
55 3 occupation Comm midwife
56 3 wp_user_level 0
57 3 nick_name Virginia
58 3 last_name Schodde
59 3 first_name Virginia
60 3 title
61 3 country
63 3 isinstructor
64 3 gender
65 3 idf_indig_tsi
66 3 idf_ct_ld
67 3 comments
68 3 action
69 3 user_id 3
70 3 submit Save
72 3 traininglocation Adelaide
46980 3 workplace Noarlunga Health service
45304 3 trainingdate 22/03/2005
48658 3 contact_no 0883849673
43616 3 wp_capabilities a:1:{s:10:"subscriber";s:1:"1";}
41939 3 maternitybg Yes
Below is my current SQL but it's not filtering by postcode 3000:
select distinct user_id from wp_usermeta where
( meta_key='state' and meta_value= 'Vic') or
( meta_key='postcode' and meta_value= '3000') or
( meta_key='maternitybg' and meta_value= 'No')
GROUP BY user_id HAVING COUNT(meta_key) = 3
Any ideas?
User number 3 has two rows for the maternitybg = Yes key, so this matches:
having count(meta_key) = 3
To fix this, add distinct to verify that it matches three different keys:
having count(distinct meta_key) = 3
This is my table structure:
rec_id product_id quantity quantity_in quantity_out balance stock_date status
1 2 342 NULL 17 325 2009-10-23 1
2 2 325 NULL 124 201 2009-10-23 1
3 1 156 NULL 45 111 2009-10-23 1
4 2 201 NULL 200 1 2009-10-23 1
5 2 1 NULL 1 0 2009-10-23 1
6 1 111 NULL 35 76 2009-10-23 1
All I want is the last transaction done for a given product: product_id, quantity, quantity_out and balance from this table.
Example, there are 2 transaction done for product 2 (ids 1 & 2):
final balance for product_id 2 is 0 -> stored in rec_id 5
final balance for product_id 1 is 76 -> stored in rec_id 6
Final result/output should be like this:
recid productid quantity quantityin quantityout balance stock_date status
5 2 1 NULL 1 0 2009-10-23 1
6 1 111 NULL 35 76 2009-10-23 1
You can find the latest record for each product like:
select max(rec_id) as MaxRec
from YourTable
group by product_id
Using a subquery, you can retrieve the latest rows for their product:
select *
from YourTable
where rec_id in (
select max(rec_id) as MaxRec
from YourTable
group by product_id
)
Here's a single query with no subqueries:
SELECT main.*
FROM YourTable main
LEFT JOIN YourTable newer
ON newer.product_id = main.product_id AND newer.rec_id > main.rec_id
WHERE newer.rec_id IS NULL;
You can tweak the field list however you want--make sure you select fields from main, not newer, which should be all null.