I have two table named test and result.
If candidates register then it will be inserted in test table.
If candidate completed test then details has been inserted into result table.
Now I need a query to compare two table and populate detail candidates those who are not complete the test by using candidate id. I am using zend frame work please guide me
I am using this code but it does not work
$dbTableInfo = $this->getDbTable()->info();
$select->from(array(
'c1' => 'test'),
array('c1.candidate_id',
"CONCAT( c1.first_name,
' ',
c1.last_name ) AS full_name",
'c1.active',
'c1.sendlink',
'c1.date_added',
'c1.username',
'c1.date_modified',
'c1.test_id')
);
$select->joinLeft(array('re'=>'result'));
$select->where("c1.business_id='" . $cid . "' AND 'c1.candidate_id NOT IN(re.candidate_id)'");
$select->order('c1.candidate_id');
Sorry for not answering your question, but why don't you simply use a single table with a discriminator field, which would be called type for example, and use it this way:
If a candidate registers he will be inserted with type set to test
If a candidate completed the tests then his type would be changed to completed
Maybe you forgot to specify condition In joinLeft?
from zend docs :
LEFT JOIN with the joinLeft(table, condition, [columns]) method. All
rows from the left operand table are included, matching rows from the
right operand table included, and the columns from the right operand
table are filled with NULL if no row exists matching the left table.
and in your query you didnt specify neither the condition nor the columns you want returned.
for example you can do your join like this :
$select->from(array(
'c1' => 'test'),
array('c1.candidate_id',
"CONCAT( c1.first_name,
' ',
c1.last_name ) AS full_name",
'c1.active',
'c1.sendlink',
'c1.date_added',
'c1.username',
'c1.date_modified',
'c1.test_id')
);
$select->joinLeft(
array('re'=>'result'),
'"c1".candidate_id = "re".candidate_id',
"*"
);
Related
I have a table called Listings and a table called wines and one more called wineinfo
I originally was using the following to get the info from the listings table only, how ever since I restructured my DB, it requires the use of two other tables.
$listing = $this->db->get_where( "listings", [ "listingID" => $id ]
)->row();
if( !$listing )
throw new Exception("Error: Listing you're trying to bid on does not exist.", 1);
// check not to bid on own listings
if( $listing->list_uID == $userid )
throw new Exception("Error: Dont't bid on your own listings.", 1);
I then tried changing the code so the JOIN statements could work
$this->db->select("FROM listings.*, Vintage, Vineyard, Wine_Name, Region, Advice, Grape,Producer,Type id,wine_id,Wine_Name,");
$this->db->from("wineinfo");
$this->db->where(["listingsID" => $id]);
$this->db->where(["wineinfo.wine_id" => "listings.wine_id"]);
$this->db->where(["wineinfo.Vintage" => "listings.wine_id"]);
$this->db->join("wines", "wineinfo.wine_id = wines.wine_id");
$listing = $this->db->get()->row();
I am being given this error.
Unknown table 'listings'
But there is 100% a table called listings.
I know I've missed something, or definitely messed up the code, I am only just learning about this and the code above worked for something else, but now I've amended it to this, it hasn't.
I then tried changing the code so the JOIN statements could work
you are trying to combine 3 tables with 2 FROM and one JOIN clauses, which is incorrect the way you do it.
you need to keep SELECT clean, just select the columns you need, like:
$this->db->select("listings.*, wineinfo.*, wine.*");
then the FROM clause:
$this->db->from("wineinfo");
then make the joins:
$this->db->join("listings", "wineinfo.wine_id = listings.listingsID");
$this->db->join("wines", "wineinfo.wine_id = wines.wine_id");
followed by your where clauses.
please note, I don't know your table structure, so I can only guess your JOIN relationships. Also this is a simplified example, where I suppose that the 3 tables don't have matching column names.
response to "ambiguous" comment: you can limit your select clause to just necessary columns, e.g.
$this->db->select("listings.*, wineinfo.vintage, wine.*");
or use an alias:
$this->db->select("listings.*, wineinfo.wine_id as my_wineID, wine.*");
The FROM in $this->db->select("FROM listings.*, Vintage, Vineyard, Wine_Name, Region, Advice, Grape,Producer,Type id,wine_id,Wine_Name,"); is not supposed to be there.
The generated out query now looks like:
SELECT FROM *... FROM sometable
My Problem: Getting query results from 2 db tables with PROPEL2 even when second table has no corresponding entries. If the second has corresponding entries than it is no problem.
I have 3 tables: Entry, Contingent and Favorit.
The schema is as follow:
Entry.id [PK]
Entry.contingent_id [FK]
Entry.expert_id
Contingent.id [PK]
Contingent.name
Favorit.id [PK]
Favorit.contingent_id [FK]
Favorit.expert_id
Favorit.pos
I want to get for a specified expert_id ($id) all entries from Entry with contingent-name and if exists the favorit.pos for this expert and contingent. I get the wanted with:
$result = EntryQuery::create()
->filterByExpertId($id)
->join('Entry.Contingent')
->withColumn('Contingent.name','_contingentName')
->join('Contingent.Favorit')
->where('Favorit.expert_id = ?', $id)
->find();
This works only if there exists such a favorit.pos . In some cases this element doesn’t exists (what is wanted from the system). In these cases I want to get the result too just with favorit.pos as empty, null or 0. But Propel doesn’t return me these records.
With MySQL I have no problem to get the desired result:
SELECT entry.* ,
(SELECT favorit.position
FROM contingent, favorit
WHERE
favorit.expert_id = entry.expert_id
AND entry.contingent_id = contingent.id
AND contingent.id = favorit.contingent_id
)
FROM `entry`
JOIN contingent
ON entry.contingent_id = contingent.id
WHERE
entry.expert_id=1;
Use Join left in code:
->join('Contingent.Favorit','selection conditon','left' )
This left work when empty database when condition is false
in condition like 'id'=$id
I need to concat column name depending on row value. Below is my table format:-
Table Name = tbl_occupancy
so from this table, I need to concat below column if their value is 1:-
working_people
owner_occupied
students
dss_referrals
local_authority
output would be in below format:-
Working People/Owner Occupied/Students/Dss Referrals/Local Authority
when all of these columns value are one.
Suppose if working_people & dss_referrals = 1 then my output will be below:-
Working People/Dss Referrals
trying for one day. not found any solution till now.
I need this output in a single row mentioned above. Because this query will work as an sub query in my report module. And this output will be shown as a column in xls sheet.
Any help plz.
You can use CASE inside the CONCAT() function
SELECT
DISTINCT risk_reference,
CONCAT(
CASE WHEN `working_people` = 1 THEN 'Working People /' ELSE '' END ,
CASE WHEN `owner_occupied` = 1 THEN 'Owner Occupied /' ELSE '' END ,
CASE WHEN `students` = 1 THEN 'Students /' ELSE '' END ,
CASE WHEN `dss_referrals` = 1 THEN 'Dss Referrals/' ELSE '' END ,
CASE WHEN `local_authority` = 1 THEN 'Local Authority' ELSE '' END
) `concat_columns`
FROM
`table`
EDIT:
You can use DISTINCT to get the distinct results but in your table you have multiple rows so there will be multiple results although you can use a GROUP BY risk_reference but its not good to use without aggregate functions
I am using below query for my solution:-
select group_concat("/",col.column_name) as hazards from information_schema.columns
as col inner join db_name.`tbl_occupancy` as oc on col.table_schema="db_name"
and col.table_name = "tbl_occupancy" and col.column_name in (
if(oc.working_people=1,"working_people",""),
if(oc.owner_occupied=1,"owner_occupied",""),
if(oc.students=1,"students",""),
if(oc.dss_referrals=1,"dss_referrals",""),
if(oc.local_authority=1,"local_authority","")
);
You can Use
"concat-ws"
. It does not skip empty strings. However, it does skip any NULL values after the separator and argument.
`mysql> SELECT CONCAT_WS(',','Test',NULL,'1');
output -> 'Test,1'`
You could try like:
SELECT CONCAT_WS("/",
IF(working_people=1,"working_people",""),
IF(owner_occupied=1,"owner_occupied"),
IF(students=1,"students",""),
IF(dss_referrals=1,"dss_referrals",""),
IF(local_authority=1,"local_authority","")) AS result
FROM tbl_occupancy;
I'm trying to write up an email notification system for a job recruitment site I made and am currently looking at grouping a certain amount of jobs together before sending an email to the candidate
I have a table which I've called candidate_to_job which contains the candidate_id, job_id and an "emailed" boolean
What I'm struggling with is updating that table when a new job is posted and emails are sent out. So far when a new job is posted I run the following:
SELECT c2j.candidate_id, c2j.job_id, j.title
FROM " . DB_PREFIX . "candidate_to_job c2j
LEFT JOIN " . DB_PREFIX . "job j
ON (j.job_id = c2j.job_id)
WHERE c2j.emailed = 0
Then through PHP I group them all together so I have an array looking something like this:
$candidates = array(
1 => array(1,2,3),
2 => array(1,3),
3 => array(4,5,6)
);
With the array key being the candidate ID and the value an array of job IDs
What I want to do using that array - after the emails have been sent - is update the candidate_to_job table setting emailed to true, e.g candidate_id 1 would have emailed set to true for job_ids 1, 2 and 3
Is there a way I can do this in one query? I've looked at WHEN CASE statements but I don't think that applies in this case? I really don't want to run multiple queries per candidate because there could potentially be thousands!
You can run one UPDATE query per group provided that the group can share the same WHERE criteria and the same update values.
UPDATE tbl SET value = TRUE WHERE id IN(1,2,3,4,5,6);
UPDATE tbl SET value = FALSE WHERE id IN(7,8,9,10,11);
Or you can use the WHEN clause or even some IF clauses provided that the criteria are simple enough.
UPDATE tbl SET value = IF(id = 1) WHERE id IN(1,2);
UPDATE tbl
SET value = CASE
WHEN id IN (1,2,3,4,5,6) THEN TRUE
WHEN id IN (7,8,9,10,11) THEN FALSE
ELSE value
END
WHERE id IN (1,2,3,4,5,6,7,8,9,10,11);
Having possibly thousands of WHEN cases, may be a hassle to build/change, I'd go with the first option:
Flip the old key=>value array and keep all ids connected to a value:
foreach($list AS $id => $value) {
$list2[$value][] = $id;
}
Iterate through the value=>keys array and build UPDATE queries that can bulk update the value for all keys at once.
If you are using mysqli extension in PHP instead of mysql, you can also write mutliple statements seperated by ';' and send all of them in one query. This might result in slightly simpler code than a complex UPDATE with cases.
I dont think this costs much more performance than one single complex UPDATE statement.
Is this what you're after? You can do a join using from and where without having to dynamically generate SQL in PHP.
UPDATE " . DB_PREFIX . "candidate_to_job c2j
SET emailed = 1
FROM " . DB_PREFIX . "job j
WHERE j.job_id = c2j.job_id and
c2j.emailed = 0
I have two primary MySQL tables (profiles and contacts) with many supplementary tables (prefixed by prm_). They are accessed and manipulated via PHP.
In this instance I am querying the profiles table where I will retrieve an Owner ID and a Breeder ID. This will then be referenced against the contacts table where the information on the Owners and Breeders is kept.
I received great help here on another question regarding joins and aliases, where I was also furnished with the following query. Unfortunately, I am having huge difficulty in actually echoing out the results. Every single site that deals with Self Joins and Aliases provide lovely examples of the queries - but then skip to "and this Outputs etc etc etc". How does it output????
SELECT *
FROM (
SELECT *
FROM profiles
INNER JOIN prm_breedgender
ON profiles.ProfileGenderID = prm_breedgender.BreedGenderID
LEFT JOIN contacts ownerContact
ON profiles.ProfileOwnerID = ownerContact.ContactID
LEFT JOIN prm_breedcolour
ON profiles.ProfileAdultColourID = prm_breedcolour.BreedColourID
LEFT JOIN prm_breedcolourmodifier
ON profiles.ProfileColourModifierID = prm_breedcolourmodifier.BreedColourModifierID
) ilv LEFT JOIN contacts breederContact
ON ilv.ProfileBreederID = breederContact.ContactID
WHERE ProfileName != 'Unknown'
ORDER BY ilv.ProfileGenderID, ilv.ProfileName ASC $limit
Coupled with this is the following PHP:
$owner = ($row['ownerContact.ContactFirstName'] . ' ' . $row['ownerContact.ContactLastName']);
$breeder = ($row['breederContact.ContactFirstName'] . ' ' . $row['breederContact.ContactLastName']);
All details EXCEPT the contacts (gender, colour, etc.) return fine. The $owner and $breeder variables are empty.
Any help in settling this for me would be massively appreciated.
EDIT: My final WORKING query:
SELECT ProfileOwnerID, ProfileBreederID,
ProfileGenderID, ProfileAdultColourID, ProfileColourModifierID, ProfileYearOfBirth,
ProfileYearOfDeath, ProfileLocalRegNumber, ProfileName,
owner.ContactFirstName AS owner_fname, owner.ContactLastName AS owner_lname,
breeder.ContactFirstName AS breeder_fname, breeder.ContactLastName AS breeder_lname,
BreedGender, BreedColour, BreedColourModifier
FROM profiles
LEFT JOIN contacts AS owner
ON ProfileOwnerID = owner.ContactID
LEFT JOIN contacts AS breeder
ON ProfileBreederID = breeder.ContactID
LEFT JOIN prm_breedgender
ON ProfileGenderID = prm_breedgender.BreedGenderID
LEFT JOIN prm_breedcolour
ON ProfileAdultColourID = prm_breedcolour.BreedColourID
LEFT JOIN prm_breedcolourmodifier
ON ProfileColourModifierID = prm_breedcolourmodifier.BreedColourModifierID
WHERE ProfileName != 'Unknown'
ORDER BY ProfileGenderID, ProfileName ASC $limit
Which I could then output by:
$owner = ($row['owner_lname'] . ' - ' . $row['owner_fname']);
Many Thanks to All!
I guess you're using the mysql_fetch_array or the mysql_fetch_assoc-functions to get the array from the result-set?
In this case, you can't use
$row['ownerContact.ContactFirstName']
as the PHP-Docs read:
If two or more columns of the result have the same field names, the
last column will take precedence. To access the other column(s) of the
same name, you must use the numeric index of the column or make an
alias for the column. For aliased columns, you cannot access the
contents with the original column name.
So, you can either use an AS in your SQL-query to set other names for the doubled rows or use the numbered indexes to access them.
This could then look like this:
Using AS in your Query
In your standard SQL-query, the columns in the result-set are named like the columns which their values come from. Sometimes, this can be a problem due to a naming-conflict. Using the AS-command in your query, you can rename a column in the result-set:
SELECT something AS "something_else"
FROM your_table
This will rename the something-column to something_else (you can leave the ""-quotes out, but I think it makes it more readable).
Using the column-indexes for the array
The other way to go is using the column-index instead of their names. Look at this query:
SELECT first_name, last_name
FROM some_table
The result-set will contain two columns, 0 ==> first_name and 1 ==> last_name. You can use this numbers to access the column in your result-set:
$row[0] // would be the "first_name"-column
$row[1] // would be the "last_name"-column
To be able to use the column-index, you'll need to use mysql_fetch_row or the mysql_fetch_assoc-function, which offers an associative array, a numeric array, or both ("both" is standard).
you need to replace the * with the data you need , and the similar ones you have to make aliases too :
ownerContact.ContactFirstName as owner_ContactFirstName
and
breederContact.ContactFirstName as breeder_ContactFirstName .
like this :
select ownerContact.ContactFirstName as owner_ContactFirstName , breederContact.ContactFirstName as breeder_ContactFirstName from profiles join ownerContact ... etc
in this way you will write :
$owner = ($row['owner_ContactFirstName'] . ' ' . $row['owner_ContactLastName']);
$breeder = ($row['breeder_ContactFirstName'] . ' ' . $row['breeder_ContactLastName']);
You cannot specify table alias when you access row using php. Accessing it by $row['ContactFirstName'] would work if you didn't have 2 fields with the same name. In this case whatever ContactFirstName appears second overwrites the first.
Change your query to use fields aliases, so you can do $owner = $row['Owner_ContactFirstName'].
Another option I'm not 100% sure is to access field by index, not by name(e.g. $owner=$row[11]). Even if it works I don't recommend to do so, you will have a lot of troubles if change your query a bit.
On outer select You have only two tables:
(inner select) as ilv
contacts as breederContact
there is no ownerContact at all