update Mysql table with values from another mysql table - mysql

I have two tables in a mysql database. Memebers and ClassNumbers. Both tables have a paid year field and both have a code field. I want to update the paid year in the Members table with the values from the code numbers table Where code in members is the same as code in ClassNumbers.
I know this is probably nowhere near right but it might explain it a bit better.
UPDATE Members SET paidYear (THE VALUE OF PAID YEAR IN ClassNumbers) where cl_code == code
Members
id,
Paid year,
cl_code,
ClassNumbers
id,
paidyear,
code,
I came across this code when searching other answers I'm thinking it's something similar
UPDATE business b, people p
SET b.email = p.email
WHERE b.business_id = p.business_id
AND p.sort_order = '1'
AND b.email = ''

UPDATE Members JOIN ClassNumbers ON Members.cl_code = ClassNumbers.code
SET Members.paidyear = ClassNumbers.paidyear

Related

Data base One To Many Relationship Query

I have 3 tables in my DB; Transactions, transaction_details, and accounts - basically as below.
transactions :
id
details
by_user
created_at
trans_details :
id
trans_id (foreign key)
account_id
account_type (Enum -[c,d])
amount
Accounts :
id
sub_name
In each transaction each account may be creditor or debtor. What I'm trying to get is an account statement (ex : bank account movements) so I need to query each movement when the account is type = c (creditor) or the account type is = d (debtor)
trans_id, amount, created_at, creditor_account, debtor_account
Update : I tried the following query but i get the debtor column values all Null!
SELECT transactions.created_at,trans_details.amount,(case WHEN trans_details.type = 'c' THEN sub_account.sub_name END) as creditor,
(case WHEN trans_details.type = 'd' THEN sub_account.sub_name END) as debtor from transactions
JOIN trans_details on transactions.id = trans_details.trans_id
JOIN sub_account on trans_details.account_id = sub_account.id
GROUP by transactions.id
After the help of #Jalos I had to convert the query to Laravel which also toke me 2 more hours to convert and get the correct result :) below is the Laravel code in case some one needs to perform such query
I also added between 2 dates functionality
public function accountStatement($from_date,$to_date)
{
$statemnt = DB::table('transactions')
->Join('trans_details as credit_d',function($join) {
$join->on('credit_d.trans_id','=','transactions.id');
$join->where('credit_d.type','c');
})
->Join('sub_account as credit_a','credit_a.id','=','credit_d.account_id')
->Join('trans_details as debt_d',function($join) {
$join->on('debt_d.trans_id','=','transactions.id');
$join->where('debt_d.type','d');
})
->Join('sub_account as debt_a','debt_a.id','=','debt_d.account_id')
->whereBetween('transactions.created_at',[$from_date,$to_date])
->select('transactions.id','credit_d.amount','transactions.created_at','credit_a.sub_name as creditor','debt_a.sub_name as debtor')
->get();
return response()->json(['status_code'=>2000,'data'=>$statemnt , 'message'=>''],200);
}
Your transactions table denotes transaction records, while your accounts table denotes account records. Your trans_details table denotes links between transactions and accounts. So, since in a transaction there is a creditor and a debtor, I assume that trans_details has exactly two records for each transaction:
select transactions.id, creditor_details.amount, transactions.created_at, creditor.sub_name, debtor.sub_name
from transactions
join trans_details creditor_details
on transactions.id = creditor_details.trans_id and creditor_details.account_type = 'c'
join accounts creditor
on creditor_details.account_id = creditor.id
join trans_details debtor_details
on transactions.id = debtor_details.trans_id and debtor_details.account_type = 'd'
join accounts debtor
on debtor_details.account_id = debtor.id;
EDIT
As promised, I am looking into the query you have written. It looks like this:
SELECT transactions.id,trans_details.amount,(case WHEN trans_details.type = 'c' THEN account.name END) as creditor,
(case WHEN trans_details.type = 'd' THEN account.name END) as debtor from transactions
JOIN trans_details on transactions.id = trans_details.trans_id
JOIN account on trans_details.account_id = account.id
GROUP by transactions.id
and it is almost correct. The problem is that due to the group-by MySQL can only show a single value for each record for creditor and debtor. However, we know that there are exactly two values for both: there is a null value for creditor when you match with debtor and a proper creditor value when you match with creditor. The case for debtor is similar. My expectation for this query would have been that MySQL would throw an error because you did not group by these computed case-when fields, yet, there are several values, but it seems MySQL can surprise me after so many years :)
From the result we see that MySQL probably found the first value and used that both for creditor and debtor. Since it met with a creditor match as a first match, it had a proper creditor value and a null debtor value. However, if you write bullet-proof code, you will never meet these strange behavior. In our case, doing some minimalistic improvements on your code transforms it into a bullet-proof version of it and provides correct results:
SELECT transactions.id,trans_details.amount,max((case WHEN trans_details.type = 'c' THEN account.name END)) as creditor,
max((case WHEN trans_details.type = 'd' THEN account.name END)) as debtor from transactions
JOIN trans_details on transactions.id = trans_details.trans_id
JOIN account on trans_details.account_id = account.id
group by transactions.id
Note, that the only change I did with your code is to wrap a max() function call around the case-when definitions, so we avoid the null values, so your approach was VERY close to a bullet-proof solution.
Fiddle: http://sqlfiddle.com/#!9/d468dc/10/0
However, even though your thought process was theoretically correct (theoretically there is no difference between theory and practice, but in practice they are usually different) and some slight changes are transforming it into a well-working code, I still prefer my query, because it avoids group by clauses, which can be useful, if necessary, but here it's unnecessary to do group by, which is probably better in terms of performance, memory usage, it's easier to read and keeps more options open for you for your future customisations. Yet, your try was very close to a solution.
As about my query, the trick I used was to do several joins with the same tables, aliasing them and from that point differentiating them as if they were different tables. This is a very useful trick that you will need a lot in the future.

MySQL DISTINCT returning not so distinct results

Good day,
I have a small issue with MySQL Distinct.
Trying the following query in my system :
SELECT DISTINCT `booking_id`, `booking_ticket`, `booking_price`, `bookingcomment_id`, `bookingcomment_message` FROM `mysystem_booking`
LEFT JOIN `mysystem_bookingcomment` ON `mysystem_booking`.`booking_id` = `mysystem_bookingcomment`.`bookingcomment_link`
WHERE `booking_id` = 29791
The point is that there are bookings like 29791 that have many comments added.
Let's say 10. Then when running the above query I see 10 results instead of one.
And that's not the way DISTINCT supposes to work.
I simply want to know if there are any comments. If the comment ID is not 0 then there is a comment. Of course I can add COUNT(blabla) as comment_number but that's a whole different story. For me now I'd like just to have this syntax right.
You may try aggregating here, to find which bookings have at least a single comment associated with them:
SELECT
b.booking_id,
b.booking_ticket,
b.booking_price
FROM mysystem_booking b
LEFT JOIN mysystem_bookingcomment bc
ON b.booking_id = bc.bookingcomment_link
WHERE
b.booking_id = 29791
GROUP BY
b.booking_id
HAVING
COUNT(bc.bookingcomment_link) > 0;
Note that depending on your MySQL server mode, you might have to also add the booking_ticket and booking_price columns to the GROUP BY clause to get the above query to run.
You can try below - using a case when expression
SELECT DISTINCT `booking_id`, `booking_ticket`, `booking_price`, `bookingcomment_id`,
case when `bookingcomment_message`<>'0' then 'No' else 'Yes' end as comments
FROM `mysystem_booking`
LEFT JOIN `mysystem_bookingcomment` ON `mysystem_booking`.`booking_id` = `mysystem_bookingcomment`.`bookingcomment_link`
WHERE `booking_id` = 29791

SQL Querying multiple tables throwing syntax error?

I was hoping someone could help me figure out what I am doing wrong with a SQL query I am trying to run from within Sequel Pro. The error message says
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.id
INNER JOIN directory_person ON directory_twitter.id = directory_person.id
WH' at line 1
And the query that I have written is
SELECT directory_twitter.id, directory_twitter.name, directory_twitter.screen_name,
directory_twitter.followers, directory_group.id, directory_group.title
FROM directory_twitter, directory_group
INNER JOIN directory_person_groups
ON directory_person_groups.person_id = directory_twitter.id,
directory_person_groups.group_id = directory_group.id
INNER JOIN directory_person
ON directory_twitter.id = directory_person.id
WHERE directory_person.appears_in_directory = "1"
I am trying to get the query to return the name of the users (directory_twitter.name), their screen name (directory_twitter.screen_name), their total number of followers (directory_twitter.followers), and the name of the group they are in (directory_group.title). Unfortunately the way our tables are set up, the only way I can think to join the group to the user is by INNER JOIN-ing a third table (directory_person_groups) where the group ID and the user ID are both present.
Finally, the only users that I want to be returned by this query are those who are in our directory (WHERE directory_person.appears_in_directory = "1"). In the table directory_person, directory_person.id = directory_twitter.id.
I have been trying to figure out what my error is for hours and I've made no progress. Is everything correct except a syntax error that I am unfamiliar with? Any and help is greatly appreciated. Thank you!
EDIT: All of the columns of the tables that I'm querying are below.
directory_twitter: id, person_id (which is the same value as id), screen_name, name, followers, user_id (I'm not sure where else this selector is used in the database, it is a different value from id and person_id).
directory_group: id (different from directory_twitter.id), slug(a slug of the title), title, screen_name (the Twitter handle of the group, e.g. CNN for #cnn)
directory_person_groups: id (I'm not sure where else if anywhere this value appears in the database, it is different from both directory_twitter.id and directory_group.id), person_id, group_id
directory_person: id (the same as directory_twitter.id which is the same as directory_twitter.person_id), title (this value is different from directory_group.title), first_name, last_name, appears_in_directory
Try this query but you need to add some indexes or it will take a very long time to run. I would suggest you make the columns in WHERE clause as indexes before I run it if I were you.
SELECT DT.name, DT.screen_name, DT.followers, DG.title
FROM directory_twitter AS DT, directory_group AS DG, directory_person_groups AS DPG
WHERE DPG.person_id=DT.person_id AND DPG.group_id=DG.id AND DT.person_id IN
( SELECT DP.id
FROM directory_person AS DP
WHERE appears_in_directory='1'
) ;
Try this
SELECT DIRECTORY_TWITTER.ID,
DIRECTORY_TWITTER.NAME,
DIRECTORY_TWITTER.SCREEN_NAME,
DIRECTORY_TWITTER.FOLLOWERS,
DIRECTORY_GROUP.ID,
DIRECTORY_GROUP.TITLE
FROM DIRECTORY_TWITTER,
DIRECTORY_PERSON_GROUPS,
DIRECTORY_PERSON,
DIRECTORY_GROUP
INNER JOIN DIRECTORY_PERSON_GROUPS
ON DIRECTORY_PERSON_GROUPS.PERSON_ID = DIRECTORY_TWITTER.ID
AND DIRECTORY_PERSON_GROUPS.GROUP_ID = DIRECTORY_GROUP.ID
INNER JOIN DIRECTORY_PERSON
ON DIRECTORY_TWITTER.ID = DIRECTORY_PERSON.ID
WHERE DIRECTORY_PERSON.APPEARS_IN_DIRECTORY = "1"
I changed the comma to AND

Taking one column from MySQL joined tables

I have a query in MySQL and I am making a crystal report by using this.
Now inside the query i have a column called scan_mode and it is coming from gfi_transaction table. This scan_mode I am using in report to suppress some sections. But some times this value is coming null for some transaction ids.
So now I want to take this scan_mode as separate query so that it will work.
Can any one please help how I can modify the below query to take only scan_mode column.
SELECT
cc.cost_center_code AS cccde,
cc.name AS ccnme,gf.scan_mode,
cc.cost_center_id AS ccid,
site.name AS siteme,
crncy.currency_locale AS currency_locale,
cntry.language AS LANGUAGE,
cntry.country_name AS cntrynm,
crncy.decimal_digits AS rnd,
gf.transaction_no AS Serial_No,
brnd.name AS brand_name,
rsn.description AS reason,
gf.comment AS COMMENT,
ts.status_description AS STATUS,
DATE_FORMAT(gf.created_date,'%d/%m/%Y') AS created_date,
gf.created_by AS created_by,
IFNULL(gf.approval_no,'Not authorized') AS Trans_no,
gf.approved_date AS approval_dt,
gf.approved_by AS approved_by,gf.status AS status1,
IFNULL(loc.cost_center_code,cc.cost_center_code) AS cur_location,
gf.document_ref_no,gf.document_ref_type,
,DATE_FORMAT(document_ref_date1,'%d/%m/%Y')) AS invoice_no
FROM
gfi_transaction gf
INNER JOIN gfi_instruction gfn ON (gf.transaction_id=gfn.transaction_id)
INNER JOIN gfi_document_instruction doc ON (gf.ref_transaction_no = doc.document_instruction_id)
INNER JOIN reason rsn ON (gf.reason_id = rsn.reason_id)
INNER JOIN gfi_status ts ON (gf.status = ts.gfi_status_id)
INNER JOIN transaction_type tt ON (gf.transaction_type_id = tt.transaction_type_id)
INNER JOIN brand brnd ON(gf.brand_id=brnd.brand_id)
-- cc details
INNER JOIN cost_center cc ON (brnd.parent_brand = cc.brand_id OR gf.brand_id = cc.brand_id)
INNER JOIN site site ON(cc.site_id = site.site_id)
INNER JOIN country cntry ON (site.country_id = cntry.country_id)
INNER JOIN currency crncy ON (cntry.currency_id=crncy.currency_id)
LEFT OUTER JOIN alshaya_location_details loc ON
(gf.brand_id = loc.brand_id AND loc.cost_center_id = gf.cost_centre_id)
LEFT OUTER JOIN alshaya_location_details locto ON
(locto.cost_center_id = gf.from_cost_center_id)
WHERE
gf.transaction_id='{?TransID}'
AND rsn.transaction_type_id IN (10,11,14)
wow, that's a big query. I ran across a similar problem in a query i was building and found the if syntax to be a solution to my problem. This was also answered in this question: MYSQL SELECT WITHIN IF Statement
$psdb->query = "SELECT count, s.classid,
if (k.sic != k.siccode, k.siccode, s.siccode) as siccode,
if (k.sic != k.siccode, k.sicdesc, s.sicdesc) as sicdesc,
if (k.sic != k.siccode, k.sicslug, s.sicslug) as sicslug
FROM ...
It looks like scan_mode column comes from "gfi_transaction" table which seems to be primary table in your query. If you get null for this column then it means your table itself have NULL value for this column. Taking that separately in a query wont solve your problem. Try replacing null with a default value and handle it in code. You can add default value instead of NULL by using ifnull(scan_mode, 'default')

Get list of records from mysql database having following conditions:

Firstly i apologize for vague question title, i am not sure what to write there. Please let me know and i will update it.
Now here it goes..
I have 3
Table : target
Columns : target_id , target_employee_id, target_location_id, target_location_name, target_scheduled_on, target_exec_end_time, target_damaged, target_damaged_text
Table : employee
Columns : employee_id, employee_manager_id
Table : location
Columns : location_id, location_name
Little Explaination-> Managers role is to create a target for employee meaning a new target will have the location name and the scheduled on. Then employee will visit that location and report if there are any damages. This can happen multiple times in a month for same location.
What i need to show-> a list of targets having damages - between two dates (target_exec_end_time) and if there are multiple records of that same location then show the one having max date.
More Explaination-> There can be multiple entries of targets having same location but i need to show only one instance. target table image
I have tried my best to explain. Thanks in advance.
How about this:
select * from target join employee on target.target_employee_id = employee.employee_id
join location on target.target_location_name = location.location_name
where target_exec_end_time between <start_date> and <end_date>
and target_exec_end_time = (select max(target_exec_end_time) from location loc2
where loc2.location_name = target.location_name)
group by target.location_name;
Not sure about having both target_exec_end_time clauses in the where though. Maybe you just want the max value?
SELECT MAX(target_exec_end_time), *
FROM target
WHERE target_damaged=1
AND target_exec_end_time BETWEEN '2012-12-01' AND '2012-12-31'
GROUP BY target_location_id
select t.target_location_name,case when t.target_damaged >0
then t.target_damaged_text else 'Not Damaged' end target_damaged_text,
max(t.target_exec_end_time) from target t
inner join employee e on t.target_employee_id=e.employee_id
inner join location l on t.target_location_id=l.location_id
where t.target_exec_end_time between 'DATE1' and 'DATE2'
group by t.target_id,t.target_damaged_text