Find matching rows in amounts of 3 without targeting a specific number - php

I have two database tables.
The following query is selecting rows where the member_id and type match the query, then I am joining the two tables to get only entries that don't exist in the second, or exist but don't have NULL as portfolio_id.
This is working well.
$this->EE->db->select('submissions.entry_id');
$this->EE->db->from('submissions');
$this->EE->db->join('judging', 'submissions.entry_id = judging.entry_id', 'left');
$this->EE->db->where('submissions.member_group', $member_group);
$this->EE->db->where('submissions.type_id', '1');
$this->EE->db->where('judging.entry_id', NULL);
$this->EE->db->or_where('judging.portfolio_id IS NOT NULL');
$query = $this->EE->db->get();
The final part of my query has me completely stumped.
In the first table, a column named 'portfolio_name' can have a number between 1 and 7.
I need to retrieve 3 rows that have the same number in this column. If there are only two rows that match they must be ignored, like wise if there is 4 etc
How can this be achieved, without targeting an exact number?
Thank you

Related

php sql check value print one line for each positive record

I have been working on my "administrative" webpage for a while now, almost finished. Just need a little help with one last thing;
I want to get a value (ID) from a table posted as Yes if the ID is found, but no if it isn't.
Have managed to get it printet, but it prints all the records in the table on every person that displays on the website (http://gyazo.com/13f271bbb8c4e83ff9ecd9908545c854 where it says "Betalt" it should just be ONE correct for each one).
The code for that part is here:
if ($row[9] == $row[1]) {
$betalt = "Ja";
}
$row[9] is the value from table SI_PAYMENTS. And $row[1] is the value from table BUSS1.
These two values should be the same if the record exists in SI_PAYMENTS, and if it exists, I want the webpage to display "Yes". But if it don't find it, I want the webpage to display "No".
EDIT
Here's my SQL Query:
SELECT buss1.navn, buss1.plassnummer, buss1.telefon, buss1.epost, buss1.fodselsdato, buss1.pastigningssted, buss1.bilettype, buss1.ankommet, buss1.merknader, si_payment.ac_inv_id FROM buss1, si_payment ORDER BY buss1.plassnummer ASC
I think you need to JOIN the two tables. At the moment, you will get every row from table buss1 next to every row from si_payment. So if there are 6 rows in buss1 and 4 in si_payment, you'll end up with 24 rows. I suspect there is a column in the two tables that links them, so you need a JOIN: something like this
SELECT buss1.navn, buss1.plassnummer, buss1.telefon, buss1.epost, buss1.fodselsdato, buss1.pastigningssted, buss1.bilettype, buss1.ankommet, buss1.merknader, si_payment.ac_inv_id
FROM buss1 INNER JOIN si_payment WHERE buss1.some_column = si_payment.another_column
ORDER BY buss1.plassnummer ASC
I can't tell what some_column and another_column should be, I'm afraid. But if you change those, that will give you only the matching rows from the two tables. If that's not quite right, you may need a LEFT JOIN, which will give you all rows from the first table and the matching rows from the same table.
If I've misunderstood, I apologise.

MySQL query to update multiple rows based on a column using php

Is there a way that I could update several rows based on a column?
I have a table called category with 2 columns (Category, number).
The column category have 2 rows(Agriculture,Apparel) which is fixed. I need a mySQL update statement that could update the number column. That means 2 rows in 'number' column will have to be updated.
The code that I gave could only update one row at the time. How do I update both rows using a single query? Thanks.
My code:
$sql="UPDATE category
SET number = '$AF'
WHERE Category = 'Agriculture' ";
Not quite sure I'm understanding. If you want to update all of the rows, then remove your where clause. SQL is very much a "the less you put in the query, the more you get"-type language.
If you want to change the values of the number field to DIFFERENT values, but in a single query, then you have to do a very ugly:
UPDATE category
SET number = CASE Category
WHEN 'Agriculture' THEN $value_for_agriculture
WHEN 'Apparel' THEN $value_for_apparel
END CASE

query table row with matching numbers?

Okay I'm relatively new to php, and at the moment, I'm completely lost!
I have a page for users to compare up to three products, and at the same time, save their search to a table ("saved_results") so that that I can have a section, for lets say, recent searches, or similar searches, etc....
What happens is, as the user compare multiple products, it takes the product ids they're comparing from the products table ("products") and saves it to a new table ("compared_products") in the db, in the field compared_ids, using a comma separated format (id1, id2, id3).
how can I make a query to return fields in the "saved_results" where at least two of the comma separated fields are the same?
I'm assuming I can't use a LIKE statement, I dont think it would work because if I wanted to find a row where someone compared product 1 and 5, it would return any row with one of those numbers, including lets say 10, 11, 15, 16...25, 50,51,52.... because 1 and/or 5 are in all those numbers!
I would suggest you a little change in your table structure. To store comma separated values in table rows in a wrong fashion.
Generate a unique compare_id for each user at a particular time.
And restore your compared_products table with following structure -
id
compare_id
product_id
...
...
Hence, if one particular compare there are 3 products compared, you'll insert 3 rows instead on 1.
id compare_id product_id
1 33 52
2 33 37
3 33 11
Which'll in turn help you in querying easily.

SQL check if next row doesn't exist

I have an MsSQL server and what I am doing is selecting rows from the table. Currently I have two rows so I was wondering how would I check if there are no other rows after second one without making another query?
For example
table users
id name pass
1 joe 123
2 bob abc
How would I check if there is no row after 2 with just a query? I am willing to combining it with my current query, which just selects the data.
You can return the number of rows in your query as another column:
SELECT id, name, pass, count(*) over () as rows
FROM users
Keep in mind that this is telling you the number of rows returned by the query, not the number of rows in the table. However, if you specify a "TOP n" in your select, the rows column will give you the number of rows that would have been returned if you didn't have "Top n"
If you're trying to paginate the trick is to query one more record than you actually need for the current page. By counting the result (mysql_num_rows) and comparing that to your page size, you can then decide if there is a 'next page'.
If you were using mysql you could also use SQL_CALC_FOUND_ROWS() in your query, which calculates the number of rows that would be returned without the LIMIT clause. Maybe there is an equivalent for that in MsSQL?
I assume that you are inserting manually the first 2 rows of your table.
According to that, my idea is to just do a select where the id is more than 2 like this:
SELECT * FROM users WHERE id > 2;
I also assume that you are using PHP so mysql_num_rows will return you 0 if no data is found, otherwise it will return you the number of rows from your query and now you know that you need to do a while or some loop to retrieve the data after id number 2.

Better way of getting values from table without looping 300 queries

I have a query and a loop written that lists all the rows from a mysql table ("records") formatted in a HTML table.
One of the fields is "subject_id" which is an integer. I also have a table named "subjects" which corresponds to the same "subject_id" in the records table. Only two fields are in the "subjects" table, is an auto-index ID integer and a varchar(1024) for the subject title.
The value that returns from the "records" table is an integer. I want to perform a lookup on the integer from the "records" table for each row to output the text equivalent from the "subject_id" field.
My first notion, the kindergarten way, would be to throw in another query within the loop, effectively increasing my number of queries from 300 to 600 to load the page (no pagination is needed).
What would be a better way of this "sub query" aside from adding 300 more queries?
Edit:
I'm pulling the data from the table using a while loop and echoing my variable $r_subject (from: $r_subject = mysql_result($result,$a,"subject");). The value returned from the initial records table is INT. I want to take the $r_subject and then check it against the SUBJECTS table to get the string name associated with that INT id.
It's hard to know exactly what you need without seeing code, but from what I gather, you have 2 tables, one has the ID, the other has the text, so you would want to use a join.
The second thing is, you'll want to look at whether or not you really need 300 queries in the first place. That's a lot of queries to run and you should only need to run that many queries when you're running a bulk insert/update or something of that nature; other than that, you most likely could reduce that number substantially.
select
A.*,
B.title
from
records A,
subjects B
where
B.subject_id = A.subject_id
That's a single query that will produce all of the data you need for your page.
select
subjects.SubjectTitle,
records.whateverFieldYouWant,
records.AnyOtherField
from
records
join subjects
on records.subject_id = subjects.subject_id
where
records.subject_id = TheOneSubjectYouWant
but can't confirm without actual structure of tables and some sample data displayed showing proper context of what you are expecting out

Categories