Mixing string into the mysql query result - mysql

I have 2 tables in my database. Table users contains user_id, first_name, last_name, email. Table tickets contains information about sold tickets and there is column user_email.
What I want to do now is to take all the users who bought the ticket and join it with the users table. The problem is that as a result I need to have a list of links that look like this:
<a href='http://example{IdNumberHere}.com'> {FirstNameHere} {LastNameHere} </a>
Is it possible to have a result like this?

You can use the CONCAT() function to join strings together, so something like
SELECT CONCAT("<a href='http://example", user_id, "'> ", first_name,
" ", last_name, " </a>") AS addr
FROM ...
Should do the trick. You would then reference this concatenation as $dbRow['addr'] in your PHP output.

Related

How to use multiple "where like" in a relationship - Laravel

I have 4 tables in my DB. User , user_details, tags and taggables. By using the user table I am getting user along with user detail and tags. Following is table schema
Table user:
id, name, email, password
Table user_details
user_id, about, vision, picture
Table tags:
id, name
Table taggables
user_id, tag_id
here is my query:
User::with('userDetails','tags')->get();
I want to use where like in user name or tag name, How can I use multiple where like on user name and tag name????
Do you want something like this?
$users= User::with('userDetails','tags')
->where('name', 'LIKE',"%{$search}%")
->orWhereHas('tags', function($query) use($search) {
$query->where('name','LIKE',"%{$search}%");
})
->get();
$search is a variable here.

SQL query to get agreement list

I have some analytical data for different cases. Each case is associated with one or more photos. Each photo is analyzed by two users.
The stored data looks like
What I want is to have SQL query to generate agreement result as shown below
So, for case 17116 there is agreement on photo 175062 from user id 26 and 27. Similar case is with photo id 176031 from user id 24 and 29.
Can somebody help me out to achieve this.
Thanks for sharing your valuable time.
Here is sample data to test with
Case Id,Photo Id,FeatureCheck,Result,CheckedBy
17116,173442,severity,none,24
17116,173442,severity,low,25
17116,175062,severity,none,26
17116,175062,severity,none,27
17116,175427,severity,medium,24
17116,175427,severity,high,28
17116,175748,severity,low,22
17116,175748,severity,none,30
17116,176031,severity,low,24
17116,176031,severity,low,29
17277,175309,severity,none,24
17277,175309,severity,none,25
17277,175649,severity,none,24
17277,175649,severity,none,25
You can try below query:
select PhotoId,
max(FeatureCheck),
max(Result),
max(CheckedBy),
min(CheckedBy)
from MyTable
group by PhotoId
having count(distinct FeatureCheck) = 1
and count(distinct Result) = 1
SELECT caseid, photo_id , feature_check, agreedupon,
group_concat(checkedby SEPARATOR ',') as listusers
FROM table1
GROUP BY case_id, photo_id
Asssuming the possibility of more than 2 users checked the data. Then grouping them is more dynamic.

Outer query value is not able use in inner query

SELECT
(select Email from Contact where AccountId = Account.Id),
Id,
BillingCity,
BillingCountry,
BillingPostalCode,
BillingState,
BillingStreet,
Name,
Phone
FROM Account
where
LastModifiedDate < #[flowVars['timestamp']]
Problem here is I am not able to get the Email which is present in the sub query based on the Id of current iteration. Can you please help on this
I'm not sure how you are running the query, and how you are accessing the result, but if you are doing it in a place that does not give you the result dynamically, meaning that you try to access the columns by expected name, i.e. trying to get the "email" column somehow, then you need to fix a small issue in the query.
You need to add the AS operator to give your subquery a meaningful name like email like so:
...
(select Email from Contact where AccountId = Account.Id) as email,
...
See fiddle here for working example: db-fiddle
You can get rid of the scalar sub-query and just put a join to the CONTACT table instead. The following assumes that the CONTACT table is an optional relationship.
SELECT
con.email,
acct.Id,
acct.BillingCity,
acct.BillingCountry,
acct.BillingPostalCode,
acct.BillingState,
acct.BillingStreet,
acct.Name,
acct.Phone
FROM account acct
LEFT OUTER JOIN
contact con ON con.account_id = acct.account_id
WHERE acct.LastModifiedDate < #[flowVars['timestamp']]

where clause in COUNT function and joining two queries

I have a table that I am trying to count the number of course passed, and also list the modules passed as well.
The first problem I am having is what to put in the where variable, so that its not specific to a customer(I can use the query below for a particular customer and a particular course)but I will like a generic query where the result will be distinct in terms of user and course like the one below
SELECT FirstName
,LastName
,CourseTitle
,Noofmodules
,count(Coursecompleted) AS modulescompleted
FROM EStudentsprogress
WHERE Coursecompleted = '1'
AND EmailAddress = 'scascsc#e.co.uk'
AND CourseTitle = 'Microsoft MOS 2010 EXCEL'
GROUP BY FirstName
,LastName
,CourseTitle
,Noofmodules
How can I make it list the result as above, whereby I don't specify the email address or course title(trying to get the result for all the clients )
Also I have a query that list the courses that is passed by the customer, I will like the column with the list of courses passed be added to the result above, but as a column for each course.
SELECT FirstName
,LastName
,CourseTitle
,EmailAddress
,CourseModule AS coursepassed
FROM EStudentsprogress
WHERE coursecompleted = 1
Cheers
Can you not just add the email address and course title fields to the select fields and the GROUP BY clause.
Also you can use GROUP_CONCAT to bring back a field containing all the course modules.
Something like this:-
SELECT FirstName,
LastName,
CourseTitle,
Noofmodules,
EmailAddress,
CourseTitlecount,
COUNT(Coursecompleted) as modulescompleted,
GROUP_CONCAT(CourseModule) as modulescompletednames
FROM EStudentsprogress
WHERE Coursecompleted = '1'
GROUP BY FirstName, LastName, CourseTitle, Noofmodules, EmailAddress, CourseTitlecount ;

sql query returns incorrect result

I have a mysql database that stores quotation documents with some products that are clearly defining the price of each product in them, and a table for contracts storing contract details as well as customer code and quotation code to which it belongs. I have the following query to see how much is the total price of the quotation to write it in the invoice:
select
sum(sqproducts.price * sqproducts.quantity) as 'total-price',
squotations.currency as 'currency'
from
sqproducts,
ccontracts,
squotations
where
sqproducts.contracted=1
AND squotations.code=sqproducts.quotation_code
AND sqproducts.quotation_code=ccontracts.squotation_code
AND sqproducts.quotation_code='QUOT/2012/1'
group by
currency
Its a blind hit , Try this one:
select
sum(sqproducts.price * sqproducts.quantity) as 'total-price',
squotations.currency as 'currency'
from
sqproducts
inner join squotations on (squotations.code=sqproducts.quotation_code)
inner join ccontracts on (sqproducts.quotation_code=ccontracts.squotation_code)
where
sqproducts.contracted=1
AND sqproducts.quotation_code='QUOT/2012/1'
group by
squotations.currency